protected static String save(PortletRequest request, ActionResponse response, JMSResourceData data, boolean planOnly) throws IOException {
JMSProviderData provider = JMSProviderData.getProviderData(data.rarURI, request);
if(data.objectName == null || data.objectName.equals("")) { // we're creating a new pool
//data.instanceName = data.instanceName.replaceAll("\\s", "");
DeploymentManager mgr = ManagementHelper.getManagementHelper(request).getDeploymentManager();
try {
File rarFile = PortletManager.getRepositoryEntry(request, data.getRarURI());
ConnectorDeployable deployable = new ConnectorDeployable(rarFile.toURL());
DeploymentConfiguration config = mgr.createConfiguration(deployable);
final DDBeanRoot ddBeanRoot = deployable.getDDBeanRoot();
Connector15DCBRoot root = (Connector15DCBRoot) config.getDConfigBeanRoot(ddBeanRoot);
ConnectorDCB connector = (ConnectorDCB) root.getDConfigBean(ddBeanRoot.getChildBean(root.getXpaths()[0])[0]);
EnvironmentData environment = new EnvironmentData();
connector.setEnvironment(environment);
org.apache.geronimo.deployment.service.jsr88.Artifact configId = new org.apache.geronimo.deployment.service.jsr88.Artifact();
environment.setConfigId(configId);
configId.setGroupId("console.jms");
configId.setArtifactId(data.instanceName);
configId.setVersion("1.0");
configId.setType("rar");
if(data.dependency != null && !data.dependency.trim().equals("")) {
Artifact artifact = Artifact.create(data.dependency.trim());
org.apache.geronimo.deployment.service.jsr88.Artifact dep = new org.apache.geronimo.deployment.service.jsr88.Artifact();
environment.setDependencies(new org.apache.geronimo.deployment.service.jsr88.Artifact[]{dep});
dep.setArtifactId(artifact.getArtifactId());
if(artifact.getGroupId() != null) {
dep.setGroupId(artifact.getGroupId());
}
if(artifact.getType() != null) {
dep.setType(artifact.getType());
}
if(artifact.getVersion() != null) {
dep.setVersion(artifact.getVersion().toString());
}
}
// Basic settings on RA plan and RA instance
ResourceAdapter ra;
if(connector.getResourceAdapter().length > 0) {
ra = connector.getResourceAdapter(0);
} else {
ra = new ResourceAdapter();
connector.setResourceAdapter(new ResourceAdapter[]{ra});
}
ResourceAdapterInstance raInstance = new ResourceAdapterInstance();
ra.setResourceAdapterInstance(raInstance);
raInstance.setResourceAdapterName(data.instanceName);
for (Iterator it = data.instanceProps.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String name = getPropertyName((String)entry.getKey(), provider.getInstanceConfigProperties());
for(int i=0; i<raInstance.getConfigPropertySetting().length; i++) {
if(raInstance.getConfigPropertySetting(i).getName().equals(name)) {
raInstance.getConfigPropertySetting(i).setValue((String)entry.getValue());
break;
}
}
}
GBeanLocator workManager = new GBeanLocator();
raInstance.setWorkManager(workManager);
workManager.setGBeanLink(data.workManager); //todo
// Connection Factories
if(data.getConnectionFactoryCount() > 0) {
ConnectionDefinition[] defs = new ConnectionDefinition[data.getConnectionFactoryCount()];
for (int i = 0; i < defs.length; i++) {
defs[i] = new ConnectionDefinition();
}
ra.setConnectionDefinition(defs);
for (int i = 0; i < data.getConnectionFactories().size(); i++) {
JMSConnectionFactoryData factoryData = (JMSConnectionFactoryData) data.getConnectionFactories().get(i);
JMSProviderData.ConnectionDefinition providerData = provider.getConnectionDefinitions()[factoryData.getFactoryType()];
ConnectionDefinition def = defs[i];
def.setConnectionFactoryInterface(providerData.getConnectionFactoryInterface());
ConnectionDefinitionInstance instance = new ConnectionDefinitionInstance();
def.setConnectionInstance(new ConnectionDefinitionInstance[]{instance});
if(providerData.getConnectionFactoryInterface().equals("javax.jms.ConnectionFactory")) {
instance.setImplementedInterface(new String[]{"javax.jms.QueueConnectionFactory","javax.jms.TopicConnectionFactory"});
}
instance.setName(factoryData.getInstanceName());
SinglePool pool = new SinglePool();
instance.getConnectionManager().setPoolSingle(pool);
pool.setMatchOne(true);
pool.setMaxSize(factoryData.getPoolMaxSize());
pool.setMinSize(factoryData.getPoolMinSize());
pool.setBlockingTimeoutMillis(factoryData.getPoolBlockingTimeout());
pool.setIdleTimeoutMinutes(factoryData.getPoolIdleTimeout());
if(factoryData.getTransaction().equals("none")) {
instance.getConnectionManager().setTransactionNone(true);
} else if(factoryData.getTransaction().equals("local")) {
instance.getConnectionManager().setTransactionLocal(true);
} else if(factoryData.getTransaction().equals("xa")) {
instance.getConnectionManager().setTransactionXA(true);
instance.getConnectionManager().setTransactionXACachingThread(factoryData.isXaThreadCaching());
instance.getConnectionManager().setTransactionXACachingTransaction(factoryData.isXaTransactionCaching());
}
for (Iterator it = factoryData.instanceProps.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String name = getPropertyName((String)entry.getKey(), providerData.getConfigProperties());
for(int j=0; j<instance.getConfigPropertySetting().length; j++) {
if(instance.getConfigPropertySetting(j).getName().equals(name)) {
instance.getConfigPropertySetting(j).setValue((String)entry.getValue());
break;
}
}
}
}
}
// Destinations
DDBean[] ddBeans = connector.getDDBean().getChildBean(connector.getXpaths()[0]);
AdminObjectDCB[] adminDCBs = new AdminObjectDCB[ddBeans.length];
for (int i = 0; i < adminDCBs.length; i++) {
adminDCBs[i] = (AdminObjectDCB) connector.getDConfigBean(ddBeans[i]);
}
for (int i = 0; i < data.getAdminObjects().size(); i++) {
JMSAdminObjectData admin = (JMSAdminObjectData) data.getAdminObjects().get(i);
JMSProviderData.AdminObjectDefinition providerData = provider.getAdminObjectDefinitions()[admin.getDestinationType()];
for (int j = 0; j < adminDCBs.length; j++) {
AdminObjectDCB adminDCB = adminDCBs[j];
if(adminDCB.getAdminObjectInterface().equals(providerData.getAdminObjectInterface())) {
AdminObjectInstance[] before = adminDCB.getAdminObjectInstance();
AdminObjectInstance[] after = new AdminObjectInstance[before.length+1];
System.arraycopy(before, 0, after, 0, before.length);
AdminObjectInstance instance = new AdminObjectInstance();
after[before.length] = instance;
adminDCB.setAdminObjectInstance(after);
instance.setMessageDestinationName(admin.getName());
for (Iterator it = admin.instanceProps.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String name = getPropertyName((String)entry.getKey(), providerData.getConfigProperties());
for(int k=0; k<instance.getConfigPropertySetting().length; k++) {
if(instance.getConfigPropertySetting(k).getName().equals(name)) {
instance.getConfigPropertySetting(k).setValue((String)entry.getValue());
break;
}
}
}
break;
}
}
}
// Save
if(planOnly) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
config.save(out);
out.close();
return new String(out.toByteArray(), "US-ASCII");
} else {
File tempFile = File.createTempFile("console-deployment",".xml");
tempFile.deleteOnExit();
log.debug("Writing JMS Resource deployment plan to "+tempFile.getAbsolutePath());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
config.save(out);
out.flush();
out.close();
Target[] targets = mgr.getTargets();
if (null == targets) {
throw new IllegalStateException("No target to distribute to");
}
targets = new Target[] {targets[0]};
ProgressObject po = mgr.distribute(targets, rarFile, tempFile);
waitForProgress(po);
if(po.getDeploymentStatus().isCompleted()) {
TargetModuleID[] ids = po.getResultTargetModuleIDs();
po = mgr.start(ids);
waitForProgress(po);
if(po.getDeploymentStatus().isCompleted()) {
ids = po.getResultTargetModuleIDs();
log.info("Deployment completed successfully!");
}
}
}
} catch (Exception e) {
log.error("Unable to save connection pool", e);
} finally {
if(mgr != null) mgr.release();
}
} else { // We're saving updates to an existing pool
if(planOnly) {
throw new UnsupportedOperationException("Can't update a plan for an existing deployment");
}