//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// .NET StockTrader Sample WCF Application for Benchmarking,
// Performance Analysis and Design Considerations for
// Service-Oriented Applications
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Trade.ConfigClient;
using Trade.ConfigServiceDataContract;
using Trade.ConfigServiceConfigurationSettings;
using System.Collections.Generic;
using System.IO;
using System.Web.Caching;
namespace Trade.Web
{
public partial class ConfigurationDisplay : System.Web.UI.UserControl
{
private const string BsNameCacheKey = "BSNAME";
private const string BsImageCacheKey = "BSIMAGE";
private const string OpsNameCacheKey = "OPSNAME";
private const string OpsImageCacheKey = "OPSIMAGE";
private static string ImagesDir = string.Format("Images{0}Config{0}", Path.DirectorySeparatorChar);
private static string ImagesVirtualDir = "~/Images/Config/";
private static string[] ImageExtensions = new string[] { ".gif", ".png", ".jpg" };
///
/// Gets or sets the name of the business service endpoint currently in use.
///
public string BsName {
get
{
object o = HttpRuntime.Cache[BsNameCacheKey];
return (o == null) ? string.Empty : o as string;
}
set
{
HttpRuntime.Cache.Insert(BsNameCacheKey, value);
}
}
///
/// Gets or sets the name of the order processing service endpoint currently in use.
///
public string OpsName
{
get
{
object o = HttpRuntime.Cache[OpsNameCacheKey];
return (o == null) ? string.Empty : o as string;
}
set
{
HttpRuntime.Cache.Insert(OpsNameCacheKey, value);
}
}
///
/// Gets or sets the path to a representative image for the Business Service
///
public string BsImagePath
{
get
{
object o = HttpRuntime.Cache[BsImageCacheKey];
// Image path in cache, return path
if (o != null) return o as string;
// Image path not in cache, determine image path
string bsName = BsName;
if (string.IsNullOrEmpty(bsName))
{
loadCurrentSettings();
bsName = BsName;
}
string result = buildImagePath(bsName);
// Cache result
BsImagePath = result;
return result;
}
set
{
// Cache the image path for 5 minutes beyond last access,
// invalidated by change in business service name
HttpRuntime.Cache.Insert(BsImageCacheKey, value,
new System.Web.Caching.CacheDependency(null, new string[] { BsNameCacheKey }),
System.Web.Caching.Cache.NoAbsoluteExpiration,
new TimeSpan(0, 5, 0));
}
}
///
/// Gets or sets the path to a representative image for the Order Processing Service
///
public string OpsImagePath
{
get
{
object o = HttpRuntime.Cache[OpsImageCacheKey];
// Image path in cache, return path
if (o != null) return o as string;
// Image path not in cache, determine image path
string opsName = OpsName;
if (string.IsNullOrEmpty(opsName))
{
loadCurrentSettings();
opsName = OpsName;
}
string result = buildImagePath(opsName);
// Cache result
OpsImagePath = result;
return result;
}
set
{
// Cache the image path for 5 minutes beyond last access,
// invalidated by change in business service name
HttpRuntime.Cache.Insert(OpsImageCacheKey, value,
new System.Web.Caching.CacheDependency(null, new string[] { OpsNameCacheKey }),
System.Web.Caching.Cache.NoAbsoluteExpiration,
new TimeSpan(0, 5, 0));
}
}
protected void Page_Load(object sender, EventArgs e)
{
this.CachePolicy.Dependency = new CacheDependency(null, new string[] {
BsNameCacheKey, OpsNameCacheKey });
if (this.BsImagePath != string.Empty)
{
businessServiceImage.Visible = true;
businessServiceImage.ImageUrl = this.BsImagePath;
businessServiceLabel.Visible = false;
}
else
{
businessServiceLabel.Visible = true;
businessServiceLabel.Text = this.BsName;
businessServiceImage.Visible = false;
}
if (this.OpsImagePath != string.Empty)
{
orderProcessingServiceImage.Visible = true;
orderProcessingServiceImage.ImageUrl = this.OpsImagePath;
orderProcessingServiceLabel.Visible = false;
}
else
{
orderProcessingServiceLabel.Visible = true;
orderProcessingServiceLabel.Text = this.OpsName;
orderProcessingServiceImage.Visible = false;
}
}
///
/// Builds a path to an image representing a service
///
/// Name of the service for which to generate an image path
/// A string representation of a URI pointing to an image, or empty if no
/// image is available
///
///
/// This method searches the images directory for an appropriate image to display
/// for the service name passed. Service names typically are in the format of "PLATFORM_SERVICE".
/// In the case that they are in this format, this method will search the image directory for
/// files named "PLATFORM.extension", where extension could be any extension in a hardcoded
/// list of extensions for image files. In the case that the service name is in any other format,
/// this method will simply search for files named "SERVICENAME", where service name is the service
/// name passed.
///
///
/// In the case that no suitable image can be found, an empty string is returned. In the case of
/// an empty string (indicating an absense of an image), the UI will simply display the moniker
/// associated with the current service implementation.
///
///
/// Portability note: This method does not do a case-insensitive search of the file system.
///
///
private string buildImagePath(string serviceName)
{
// Locate the absolute path for the images directory
string imagesAbsoluteDir = Path.Combine(Request.PhysicalApplicationPath,
ConfigurationDisplay.ImagesDir);
if (!Directory.Exists(imagesAbsoluteDir))
{
return string.Empty; // Image url could not be created because image does not exist
}
// Determine the file name for which to look
string fileName = serviceName;
if (serviceName.Contains("_") && serviceName[0] != '_')
{
fileName = serviceName.Split('_')[0];
}
// Iterate through possible extensions, looking for valid images
foreach (string ext in ConfigurationDisplay.ImageExtensions)
{
string currentFile = fileName + ext;
if (File.Exists(Path.Combine(imagesAbsoluteDir, currentFile)))
return string.Format("{0}{1}",
ConfigurationDisplay.ImagesVirtualDir,
currentFile);
}
return string.Empty;
}
///
/// Loads the current configuration from the configuration service
///
private void loadCurrentSettings()
{
ConfigServiceClient configClient = new ConfigServiceClient();
//Select the current configuration settings as the default values in the dropdown list
ClientConfigRequest existingClientConfig = new ClientConfigRequest();
existingClientConfig.ClientName = "DOTNET_CLIENT";
BsName = configClient.GetClientConfig(existingClientConfig).BSName;
BSConfigRequest existingBsConfig = new BSConfigRequest();
existingBsConfig.BSName = BsName;
OpsName = configClient.GetBSConfig(existingBsConfig).OPSName;
}
}
}