public synchronized Object select(Object hint) throws ServiceException {
Configuration repConf = null;
try {
repConf = (Configuration) hint;
} catch (ClassCastException cce) {
throw new ServiceException("",
"hint is of the wrong type. Must be a Configuration", cce);
}
String destination = null;
String protocol = null;
try {
destination = repConf.getAttribute("destinationURL");
int idx = destination.indexOf(':');
if ( idx == -1 )
throw new ServiceException("",
"destination is malformed. Must be a valid URL: "
+ destination);
protocol = destination.substring(0,idx);
} catch (ConfigurationException ce) {
throw new ServiceException("",
"Malformed configuration has no destinationURL attribute", ce);
}
try
{
String type = repConf.getAttribute("type");
String repID = destination + type;
Object reply = repositories.get(repID);
StringBuffer logBuffer = null;
if (reply != null) {
if (getLogger().isDebugEnabled()) {
logBuffer =
new StringBuffer(128)
.append("obtained repository: ")
.append(repID)
.append(",")
.append(reply.getClass());
getLogger().debug(logBuffer.toString());
}
return reply;
} else {
String key = protocol + type;
String repClass = (String) classes.get( key );
if (getLogger().isDebugEnabled()) {
logBuffer =
new StringBuffer(128)
.append("obtained repository: ")
.append(repClass)
.append(" to handle: ")
.append(protocol)
.append(",")
.append(type);
getLogger().debug( logBuffer.toString() );
}
// If default values have been set, create a new repository
// configuration element using the default values
// and the values in the selector.
// If no default values, just use the selector.
Configuration config;
Configuration defConf = (Configuration)defaultConfigs.get(key);
if ( defConf == null) {
config = repConf;
}
else {
config = new DefaultConfiguration(repConf.getName(),
repConf.getLocation());
copyConfig(defConf, (DefaultConfiguration)config);
copyConfig(repConf, (DefaultConfiguration)config);
}
try {
reply = this.getClass().getClassLoader().loadClass(repClass).newInstance();
if (reply instanceof LogEnabled) {
setupLogger(reply);
}
ContainerUtil.contextualize(reply,context);
ContainerUtil.service(reply,m_manager);
if (reply instanceof Composable) {
final String error = "no implementation in place to support Composable";
getLogger().error( error );
throw new IllegalArgumentException( error );
}
ContainerUtil.configure(reply,config);
ContainerUtil.initialize(reply);
repositories.put(repID, reply);
if (getLogger().isInfoEnabled()) {
logBuffer =
new StringBuffer(128)
.append("added repository: ")
.append(repID)
.append("->")
.append(repClass);
getLogger().info(logBuffer.toString());
}
return reply;
} catch (Exception e) {
if (getLogger().isWarnEnabled()) {
getLogger().warn( "Exception while creating repository:" +
e.getMessage(), e );
}
throw new
ServiceException("", "Cannot find or init repository",
e);
}
}
} catch( final ConfigurationException ce ) {
throw new ServiceException("", "Malformed configuration", ce );
}
}