final Uuid serviceID,
final ServiceBeanManager jsbManager,
final ServiceBeanContainer container) throws ServiceBeanInstantiationException {
ServiceBeanFactory.Created created = null;
MarshalledInstance marshalledProxy = null;
ServiceBeanContext context;
CommonClassLoader commonCL = CommonClassLoader.getInstance();
ComputeResource computeResource = container.getComputeResource();
/*
* Provision service jars
*/
URL[] exports = new URL[0];
URL[] implJARs = new URL[0];
if(System.getProperty("StaticCybernode")==null) {
try {
Resolver resolver = ResolverHelper.getResolver();
boolean install = computeResource.getPersistentProvisioning();
Map<String, ProvisionedResources> serviceResources = provisionService(sElem, resolver, install);
ProvisionedResources dlPR = serviceResources.get("dl");
ProvisionedResources implPR = serviceResources.get("impl");
if(dlPR.getJars().length==0 && dlPR.getArtifact()!=null) {
String convertedArtifact = dlPR.getArtifact().replaceAll(":", "/");
String[] artifactParts = convertedArtifact.split(" ");
List<URL> exportURLList = new ArrayList<URL>();
for(String artifactPart : artifactParts) {
// TODO: if the repositories is default maven central, still need to add?
exportURLList.add(new URL("artifact:"+artifactPart+dlPR.getRepositories()));
}
exports = exportURLList.toArray(new URL[exportURLList.size()]);
} else {
exports = dlPR.getJars();
}
implJARs = implPR.getJars();
} catch(Exception e) {
throw new ServiceBeanInstantiationException("Unable to provision JARs for " +
"service "+ ServiceLogUtil.logName(sElem), e);
}
}
final Thread currentThread = Thread.currentThread();
ClassLoader currentClassLoader = currentThread.getContextClassLoader();
Uuid serviceIDToUse = serviceID;
try {
ClassBundle jsbBundle = sElem.getComponentBundle();
List<URL> urlList = new ArrayList<URL>();
/*
URL[] implJARs;
if(jsbBundle!=null && jsbBundle.getCodebase()!=null)
implJARs = jsbBundle.getJARs();
else
implJARs = new URL[0];
*/
urlList.addAll(Arrays.asList(implJARs));
/* Get matched PlatformCapability jars to load */
PlatformCapability[] pCaps = computeResource.getPlatformCapabilities();
PlatformCapability[] matched = ServiceElementUtil.getMatchedPlatformCapabilities(sElem, pCaps);
for (PlatformCapability pCap : matched) {
URL[] urls = PlatformCapabilityLoader.getLoadableClassPath(pCap);
for(URL url : urls) {
if(!urlList.contains(url))
urlList.add(url);
}
}
URL[] classpath = urlList.toArray(new URL[urlList.size()]);
Properties metaData = new Properties();
metaData.setProperty("opStringName", sElem.getOperationalStringName());
metaData.setProperty("serviceName", sElem.getName());
ServiceClassLoader jsbCL = new ServiceClassLoader(ServiceClassLoader.getURIs(classpath),
new ClassAnnotator(exports),
commonCL,
metaData);
/*
ServiceClassLoader jsbCL =
new ServiceClassLoader(classpath, annotator, commonCL);
*/
currentThread.setContextClassLoader(jsbCL);
if(logger.isDebugEnabled()) {
StringBuilder buffer = new StringBuilder();
if(implJARs.length==0) {
buffer.append("<empty>");
} else {
for(int i=0; i<implJARs.length; i++) {
if(i>0)
buffer.append(", ");
buffer.append(implJARs[i].toExternalForm());
}
}
String className = (jsbBundle==null?"<not defined>": jsbBundle.getClassName());
if(logger.isDebugEnabled()) {
logger.debug("Create ServiceClassLoader for {}, classpath {}, codebase {}",
className, buffer.toString(), jsbCL.getClassAnnotation());
}
}
/* Get the servicePolicyFile from the environment. If the
* property has not been set use the policy set for the VM */
String servicePolicyFile = System.getProperty("rio.service.security.policy",
System.getProperty("java.security.policy"));
if(logger.isTraceEnabled()) {
logger.trace("{} Service security policy file {}",
ServiceLogUtil.logName(sElem), servicePolicyFile);
}
if(servicePolicyFile!=null) {
if(logger.isTraceEnabled()) {
logger.trace("Set {} service security to LoaderSplitPolicyProvider", ServiceLogUtil.logName(sElem));
}
DynamicPolicyProvider service_policy = new DynamicPolicyProvider(new PolicyFileProvider(servicePolicyFile));
LoaderSplitPolicyProvider splitServicePolicy = new LoaderSplitPolicyProvider(jsbCL,
service_policy,
new DynamicPolicyProvider(initialGlobalPolicy));
globalPolicy.setPolicy(jsbCL, splitServicePolicy);
}
/* Reload the shared configuration using the service's classloader */
//String[] configFiles = container.getSharedConfigurationFiles().toArray(new String[sharedConfigurationFiles.size()]);
Configuration sharedConfiguration = container.getSharedConfiguration();
/* Get the ServiceBeanContextFactory */
ServiceBeanContextFactory serviceBeanContextFactory =
(ServiceBeanContextFactory)Config.getNonNullEntry(sharedConfiguration,
CONFIG_COMPONENT,
"serviceBeanContextFactory",
ServiceBeanContextFactory.class,
new ServiceContextFactory());
/* Create the ServiceBeanContext */
context = serviceBeanContextFactory.create(sElem,
jsbManager,
computeResource,
sharedConfiguration);
/* Add a temporary startup value, used to check when issuing
* lifecycle notification (RIO-141) */
Map<String, Object> configParms = context.getServiceBeanConfig().getConfigurationParameters();
configParms.put(Constants.STARTING, true);
context.getServiceBeanConfig().setConfigurationParameters(configParms);
/*
* Initialize any configured Logger instances. If there are any
* exceptions loading the configurations, log the appropriate
* message and continue
*/
LoggerConfig[] loggerConfigs = context.getServiceBeanConfig().getLoggerConfigs();
if(loggerConfigs != null) {
for (LoggerConfig loggerConfig : loggerConfigs) {
try {
loggerConfig.getLogger();
} catch (Throwable t) {
logger.warn("Loading LoggerConfig", t);
}
}
}
/* Get the ServiceBeanFactory */
ServiceBeanFactory serviceBeanFactory =
(ServiceBeanFactory)Config.getNonNullEntry(context.getConfiguration(),
CONFIG_COMPONENT,
"serviceBeanFactory",
ServiceBeanFactory.class,
new DefaultServiceBeanFactory());
if(logger.isTraceEnabled()) {
logger.trace("service = {}, serviceBeanFactory = {}",
ServiceLogUtil.logName(sElem), serviceBeanFactory);
}
created = serviceBeanFactory.create(context);
logger.trace("Created ServiceBeanFactory.Created {}", created);
Object impl = created.getImpl();
logger.trace("Obtained implementation {}", impl);
if(context.getServiceElement().getComponentBundle()==null) {
String compName = impl.getClass().getName();
if(compName.indexOf(".")>0) {
int index = compName.lastIndexOf(".");
compName = compName.substring(0, index);
}
context.getServiceBeanConfig().addInitParameter(ServiceBeanActivation.BOOT_CONFIG_COMPONENT, compName);
}
/* Get the ProxyPreparer */
if(logger.isTraceEnabled()) {
logger.trace("Get the ProxyPreparer for {}", ServiceLogUtil.logName(sElem));
}
ProxyPreparer servicePreparer = (ProxyPreparer)Config.getNonNullEntry(context.getConfiguration(),
CONFIG_COMPONENT,
"servicePreparer",
ProxyPreparer.class,
new BasicProxyPreparer());
if(logger.isTraceEnabled()) {
logger.trace("Getting the proxy");
}
Object proxy = created.getProxy();
if(logger.isTraceEnabled()) {
logger.trace("Obtained the proxy %s", proxy);
}
if(proxy != null) {
proxy = servicePreparer.prepareProxy(proxy);
}
if(logger.isTraceEnabled()) {
logger.trace("Proxy {}, prepared? {}", proxy, (proxy==null?"not prepared, returned proxy was null": "yes"));
}
/*
* Set the MarshalledInstance into the ServiceBeanManager
*/
if(logger.isTraceEnabled()) {
logger.trace("Set the MarshalledInstance into the ServiceBeanManager");
}
marshalledProxy = new MarshalledInstance(proxy);
((DefaultServiceBeanManager)context.getServiceBeanManager()).setMarshalledInstance(marshalledProxy);
/*
* The service may have created it's own serviceID
*/
if(proxy instanceof ReferentUuid) {
serviceIDToUse = ((ReferentUuid)proxy).getReferentUuid();
if(logger.isDebugEnabled()) {
logger.debug("Service has provided Uuid: {}", serviceIDToUse);
}
((DefaultServiceBeanManager)context.getServiceBeanManager()).setServiceID(serviceIDToUse);
}
/*
* If the proxy is Administrable and an instanceof
* ServiceBeanControl, set the ServiceBeanControl in the
* DefaultAssociationManagement object
*/
if(proxy instanceof Administrable) {
Object adminObject = ((Administrable)proxy).getAdmin();
if(adminObject instanceof ServiceBeanControl) {
context.getAssociationManagement().setServiceBeanControl((ServiceBeanControl)adminObject);
}
}
if(logger.isTraceEnabled()) {
logger.trace("Proxy = {}", proxy);
}