Using WPS style Resource Environment Providers with Spring
Michael Porter
Lee Burch, one of our extremely talented architects, gave me a writeup on “Using WPS style Resource Environment Providers with Spring” He wanted to know where to post something like this and of course, my first thought is this blog. His justification for posting this is that while it’s a common use case in the WebSphere Portal world, many developers get it wrong. So thanks to Lee for the post.
Generally a problem most projects face is how to handle configuration information that varies between environments. Many times you can handle this by using one of WAS built in resources such as a SMTP server or a JDBC connection pool. However many times the configuration data won’t fit one of these existing WAS resource types, such as an e-mail address, a server name or a URL. To solve this a lot of approaches are available some use build tools such as Maven to build different EAR files, others use properties files located outside of the EAR. Unfortunately both of these solutions have their issues and can be difficult to manage.
To solve this problem WAS provides and WPS leverages the resource environment providers. These allow you to specify your settings as a part of the WAS console at the Cell, Node or Server level. This provides an easy way to maintain values across many servers while also doing away with the need to have special EARs built for each environment.
The following IBM article discusses this
http://www.ibm.com/developerworks/websphere/library/techarticles/0611_totapally/0611_totapally.html
Unfortunately it requires deployment to the App Server lib/ext directory, something that is not always so easily done particularly in a shared enterprise environment.
However WPS uses Resource Environment Providers but does so differently than the above article specifies, it uses the “Custom Properties” of the resource environment provider and requires no deployment to the App Server, an ideal solution.
While it is possible to bind your code directly to the fetches to the Resource Environment Provider I find a more flexible and much more modern way of addressing the problem is to use Spring and its facility for PropertyPlaceholderConfigurer.
This allows for a Spring config something like this
<bean id=”MyBean” class=”com.perficient.sample.MyBean”>
<property name=”${administratorsEmailAddress}” />
</bean>
And the property administratorsEmailAddress will be replaced with the value from the Resource Environment Provider before the context is initialized.
The whole thing would look something like this
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns=”http://www.springframework.org/schema/beans”
xmlns:util=”http://www.springframework.org/schema/util”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd”>
<bean>
<property name=”resourceProviderName” value=”NameOfMyResourceEnvironmentProvider” />
</bean>
<bean id=”MyBean” class=”com.perficient.sample.MyBean”>
<property name=”${administratorsEmailAddress}” />
</bean>
</beans>
This will lookup the custom property “administratorsEmailAddress” under the Resource Environment Provider named “NameOfMyResourceEnvironmentProvider” and do the substitution before initializing the context. Of course it also follows the standard WAS rules allowing for the Node to trump the Cell setting and the Server to trump the Node and Cell settings allowing a lot of flexibility in how the property gets propagated to your configuration.
The code for the WebSphereResourceEnvironmentProviderPlaceHolderConfigurer follows
package com.perficient.spring;
import java.util.List;
import java.util.Properties;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.ObjectName;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import com.ibm.websphere.management.AdminService;
import com.ibm.websphere.management.AdminServiceFactory;
import com.ibm.websphere.management.Session;
import com.ibm.websphere.management.configservice.ConfigService;
import com.ibm.websphere.management.configservice.ConfigServiceFactory;
import com.ibm.websphere.management.configservice.ConfigServiceHelper;
import com.ibm.websphere.management.exception.ConfigServiceException;
import com.ibm.websphere.management.exception.ConnectorException;
/**
* The Class WebSphereResourceEnvironmentProviderPlaceHolderConfigurer implements a placeholder that uses WebSphere Application Server
* Resource Environment Provider custom properties as a source. These will work in the normal way with anythign declared at the server layer
* overriding anything at the node layer which in turns overrides anything at the cell layer.</p>
* For development you may find it useful to either get rid of this definition or use a standard properties file for the placeholders. As this is
* inherited off the standard placeholder you can still use the normal properties file definition as a fallback.</p>
* To use it simply create a resource environment provider in WAS at whatever level pleases you, and then add the data you want as custom properties]
* to that. Then in spring specify the name of the resource environment provider as the “resourceProviderName” in the spring configuration
* for this WebSphereResourceEnvironmentProviderPlaceHolderConfigurer.
*
*/
public class WebSphereResourceEnvironmentProviderPlaceHolderConfigurer extends PropertyPlaceholderConfigurer {
/** The resource provider name. */
String resourceProviderName;
/** The server properties. */
Properties serverProperties = null;
/** The node properties. */
Properties nodeProperties = null;
/** The cell properties. */
Properties cellProperties = null;
/**
* Gets the resource provider name.
*
* @return the resource provider name
*/
public String getResourceProviderName() {
return resourceProviderName;
}
/**
* Sets the resource provider name.
*
* @param resourceProviderName the new resource provider name
*/
public void setResourceProviderName(String resourceProviderName) {
this.resourceProviderName = resourceProviderName;
}
/**
* Gets the properties.
*
* @param level the level
* @param providerName the provider name
* @param service the service
* @param session the session
* @return the properties
* @throws ConfigServiceException the config service exception
* @throws ConnectorException the connector exception
* @throws AttributeNotFoundException the attribute not found exception
*/
private Properties getProperties(ObjectName level, ObjectName providerName, ConfigService service, Session session) throws ConfigServiceException, ConnectorException, AttributeNotFoundException {
Properties rv = new Properties();
ObjectName sampleProviderName = null;
ObjectName[] matches;
try {
matches = service.queryConfigObjects(session, level, providerName, null);
} catch (Exception ex) {
ex.printStackTrace();
return rv;
}
if (matches.length > 0) {
sampleProviderName = matches[0]; // use the first provider found
}
if (sampleProviderName != null) {
// search for the repository provider attributes
AttributeList value = service.getAttributes(session, sampleProviderName, new String[] { “propertySet” }, false);
ObjectName propertySet = (ObjectName) ConfigServiceHelper.getAttributeValue(value, “propertySet”);
value = service.getAttributes(session, propertySet, new String[] { “resourceProperties” }, false);
List resourceProperties = (List) ConfigServiceHelper.getAttributeValue(value, “resourceProperties”);
for (int i = 0; i < resourceProperties.size(); i++) {
// load all custom properties
ObjectName on = (ObjectName) resourceProperties.get(i);
String customName = (String) service.getAttribute(session, on, “name”);
String customValue = (String)service.getAttribute(session, on, “value”);
// put it in the property set
rv.put(customName, customValue);
}
}
return rv;
}
/**
* Load environment provider properties.
*
* @throws ConfigServiceException the config service exception
* @throws ConnectorException the connector exception
* @throws AttributeNotFoundException the attribute not found exception
*/
private void loadEnvironmentProviderProperties() throws ConfigServiceException, ConnectorException, AttributeNotFoundException {
ConfigService service = ConfigServiceFactory.getConfigService();
Session session = new Session();
AdminService adminService = AdminServiceFactory.getAdminService();
// for some reason the ObjectName returned by the adminService throws a null exception if you try to query with it
// so this is just as well, server names are unique in a cell so this will always return just one server.
ObjectName server = ConfigServiceHelper.createObjectName(null, “Server”, adminService.getLocalServer().getKeyProperty(“name”) );
ObjectName[] matches = service.queryConfigObjects(session, null, server, null);
server = matches[0]; // will be exact match
ObjectName node = ConfigServiceHelper.createObjectName(null, “Node”, adminService.getNodeName());
matches = service.queryConfigObjects(session, null, node, null);
node = matches[0]; // will be exact match
ObjectName cell = ConfigServiceHelper.createObjectName(null, “Cell”, adminService.getCellName());
matches = service.queryConfigObjects(session, null, cell, null);
cell = matches[0]; // will be exact match
// direct search
ObjectName providerName = ConfigServiceHelper.createObjectName(null, “ResourceEnvironmentProvider”, “SeedlistTransformer”);
serverProperties = getProperties(server, providerName, service, session);
nodeProperties = getProperties(node, providerName, service, session);
cellProperties = getProperties(cell, providerName, service, session);
}
/**
* Resolve provider value.
*
* @param name the name
* @return the string
*/
private String resolveProviderValue(String name) {
if (serverProperties.containsKey(name) ) {
return serverProperties.getProperty(name);
} else if (nodeProperties.containsKey(name)) {
return nodeProperties.getProperty(name);
} else if (cellProperties.containsKey(name) ) {
return cellProperties.getProperty(name);
} else {
return null;
}
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#resolvePlaceholder(java.lang.String, java.util.Properties)
*/
@Override
protected String resolvePlaceholder(String placeholder, Properties props) {
if (serverProperties == null) {
try {
loadEnvironmentProviderProperties();
} catch (ConfigServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AttributeNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnectorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String value = resolveProviderValue(placeholder);
if (value == null) {
return super.resolvePlaceholder(placeholder, props);
} else {
return value;
}
}
}

--------------------- http://feedproxy.google.com/~r/PortalSolutionsBlog/~3/ZgaV8LmlQpo/ Mar 28, 2012 38 hits
| Recent Blog Posts |
27

|
Cloud Suites for the C-Suite 
Wed, Jun 19th 2013 11:19a Michael Porter IBM announced yesterday their Cloud Suites for the C-Suite. CRN also ran an article on it where they dive into the implications of it rather than the detail of “more than 100 offerings in the Cloud” that IBM delivers. I like the following quotes:
While CIOs and IT managers are often making cloud technology decisions, IBM is taking the position that C-level executives are increasingly adopting cloud applications for front-office tasks. When line-of-business executives have made th [read] Keywords: ibm
applications
database
email
networking
office
security
websphere
websphere portal
widgets
|
17

|
Adobe AEM’s Main Differentiator 
Wed, Jun 19th 2013 10:18a Robert Sumner Have you ever been asked about the differences between Adobe CQ5 and Sitecore? Sure, at first glance the stark difference is the platform…one is J2EE based and the other .NET, but apart from that, it is actually hard to tell the difference since they pretty much do the same thing. Sitecore has a user experience that most people are familiar with due to its Microsoft influence and ribbon interface, but adobe CQs runtime preview environment, along with its sidekick functionality is not too s [read] Keywords: integration
interface
microsoft
|
80

|
Loyalty 3.0 is Out 
Tue, Jun 18th 2013 6:19a Michael Porter Rajat Paharia just released his new book, Loyalty 3.0: How to Revolutionize Customer and Employee Engagement with Big Data and Gamification. Bunchball is a partner of ours and focuses on the gamification space. We’ve presented with him on this very topic several times. I just picked up my kindle version and will follow up with a review.
For now, feel free to pick up your version. I’ve always found his content interesting. [read] Keywords: linkedin
|
93

|
Do or Die Questions Boards Should Ask About Technology 
Fri, Jun 14th 2013 4:18p Michael Porter McKinsey recently posted a set of 9 questions all board should be asking about how technology is being used in their companies. It’s worth a read for all the questions and the additional detail they provide. I want to focus on the first four and some of the implications.
How will IT change the basis of competition in our industry?
What will it take to exceed our customers’ expectations in a digital world?
Do our business plans reflect the full potential of technology to improve our [read] Keywords: |
68

|
Becoming a Customer Company 
Fri, Jun 14th 2013 10:18a Michael Porter Vivek Kundra, EVP at Salesforce and former United States CIO, talked about becoming a customer
Quote from IBM 2012 CEO Study
For some time, busienss have been refining and optimizing their networks of suppliers and partners. But something revolutionary has been happening — the sudden convergence of the cloud, social, and mobile spheres — connecting customers, employee and partners in new way.
This will involve fundamentally rethinking how you connect with your customers, partners, [read] Keywords: ibm
application
community
exchange
exchange
facebook
iphone
mobile
office
|
50

|
Social Uses of Data 
Fri, Jun 14th 2013 8:18a Michael Porter I’m at the Salesforce Customer Company tour. Before they started their main session, they brought a couple people up to talk about uses of social. Here are some quick notes on those interviews.
They started with a great example of a company which provides vaccines to a variety of third world countries. They use Salesforce Chatter and sensors in vaccine refrigeration units to track that the vaccines are viable when they arrive. If you want to talk about a use of social that pushes [read] Keywords: notes
|
59

|
Social Networking Best Practices Presentation 
Tue, Jun 11th 2013 1:19p Michael Porter I presented at an eWeek sponsored event on social and social technologies in San Francisco. Here’s the presentation. [read] Keywords: application
networking
|
181

|
12 Technologies That Are Improving at Insane Speeds 
Fri, Jun 7th 2013 8:22a Mark Polly Business Insider published an article titled “12 Technologies That Are Improving at Insane Speeds” describing a McKinsey report about technologies that are disrupting the global economy. The image shown here summarizes the 12 technologies and provides a glimpse into the amount of technological improvement provided over the years.
Not surprising to me is that areas that affect Portal and Social are at the top of the list. Mobile Internet is the first example of technology that has [read] Keywords: mobile
security
|
58

|
Social Technologies: The Business Payoff 
Thu, Jun 6th 2013 1:18p Michael Porter I’m at an eWeek Sponsored half day session that focuses on Social Business. Marcia Connor was the first speaker. She’s a Principal at SensifyGroup (@marciamarcia). She focuses on helping people work at jobs that don’t suck.
One of the key problems is the sheer amount of data out there. The internet adds 34 Gigabytes each day. This information deluge means that 1 in 2 business leaders say they don’t have the information needed to do their jobs.
Our brains transf [read] Keywords: connections
notes
|
57

|
The Social Business Payoff 
Thu, Jun 6th 2013 1:18p Michael Porter Jennifer Okimoto, an Associate Partner with IBM spoke about the social business payoff and gave a lot of examples on where it’s happening.
The knowledge based economy was originally coined in 1996. It came with the realization that computers and knowledge change how the work is done. Jennifer highlighted that a knowledge worker is not just someone in a cubicle farm. It could be an oil field worker, a sales person, a chemist., people in health care, and yes, people in cubicle farms.
em [read] Keywords: collaboration
ibm
email
enterprise
networking
twitter
|
|