Configuration repConf = null;
try {
repConf = (Configuration) hint;
} catch (ClassCastException cce) {
throw new ComponentException(
"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 ComponentException(
"destination is malformed. Must be a valid URL: "
+ destination);
protocol = destination.substring(0,idx);
} catch (ConfigurationException ce) {
throw new ComponentException(
"Malformed configuration has no destinationURL attribute", ce);
}
try
{
String type = repConf.getAttribute("type");
String repID = destination + type;
MailRepository reply = (MailRepository) repositories.get(repID);
if (reply != null) {
getLogger().debug("obtained repository: " + repID
+ "," + reply.getClass());
return (Component)reply;
} else {
String repClass = (String) classes.get( protocol + type );
getLogger().debug( "Need instance of " + repClass +
" to handle: " + protocol + "," + type );
// 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(repClass);
if ( defConf == null) {
config = repConf;
}
else {
config = new DefaultConfiguration(repConf.getName(),
repConf.getLocation());
copyConfig(defConf, (DefaultConfiguration)config);
copyConfig(repConf, (DefaultConfiguration)config);
}
try {
reply = (MailRepository) Class.forName(repClass).newInstance();
if (reply instanceof LogEnabled) {
setupLogger(reply);
}
if (reply instanceof Contextualizable) {
((Contextualizable) reply).contextualize(context);
}
if (reply instanceof Composable) {
((Composable) reply).compose( componentManager );
}
if (reply instanceof Configurable) {
((Configurable) reply).configure(config);
}
if (reply instanceof Initializable) {
((Initializable) reply).initialize();
}
repositories.put(repID, reply);
getLogger().info("added repository: "+repID+"->"+repClass);
return (Component)reply;
} catch (Exception e) {
getLogger().warn( "Exception while creating repository:" +
e.getMessage(), e );
e.printStackTrace();
throw new
ComponentException("Cannot find or init repository",
e);
}
}
} catch( final ConfigurationException ce ) {
throw new ComponentException( "Malformed configuration", ce );
}
}