001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console.jmsmanager.activemqCF;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.geronimo.common.DeploymentException;
021: import org.apache.geronimo.console.util.ObjectNameConstants;
022: import org.apache.geronimo.console.util.PortletManager;
023: import org.apache.geronimo.kernel.Kernel;
024: import org.apache.geronimo.kernel.KernelRegistry;
025: import org.apache.geronimo.kernel.config.ConfigurationManager;
026: import org.apache.geronimo.kernel.config.ConfigurationUtil;
027: import org.apache.geronimo.kernel.repository.Artifact;
028: import org.apache.geronimo.kernel.repository.ListableRepository;
029:
030: import javax.portlet.PortletRequest;
031: import java.io.BufferedWriter;
032: import java.io.File;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.io.OutputStreamWriter;
036: import java.io.Writer;
037: import java.net.URISyntaxException;
038: import java.text.MessageFormat;
039: import java.util.ArrayList;
040: import java.util.Iterator;
041: import java.util.List;
042: import java.util.SortedSet;
043:
044: public class ActiveMQConnectorHelper {
045: //todo: this class is horrible and needs to be burned!
046: private final static Log log = LogFactory
047: .getLog(ActiveMQConnectorHelper.class);
048:
049: private static String MODULE_FILE;
050:
051: private final static String ACTIVEMQ_RAR = "repository/activemq/rars/activemq-ra-3.2.1.rar";
052:
053: private static final String LINE_SEP = System
054: .getProperty("line.separator");
055:
056: private static final String PLAN_TEMPLATE = getPlanTemplate();
057:
058: private static final String[] DEPLOYER_ARGS = {
059: File.class.getName(), File.class.getName() };
060:
061: private static final String DEPLOY_METHOD = "deploy";
062:
063: private static String getPlanTemplate() {
064: StringBuffer sb = new StringBuffer();
065: sb.append("<?xml version=\"1.0\"?>\n");
066: sb
067: .append("<connector xmlns=\"http://geronimo.apache.org/xml/ns/j2ee/connector-1.0\"\n");
068: sb.append(" configId=\"{0}\" parentId=\"{1}\">\n");
069: sb.append(" <resourceadapter>\n");
070: sb.append(" <resourceadapter-instance>\n");
071: sb
072: .append(" <resourceadapter-name>{2}</resourceadapter-name>\n");
073: sb
074: .append(" <config-property-setting name=\"ServerUrl\">{3}</config-property-setting>\n");
075: sb
076: .append(" <config-property-setting name=\"UserName\">{4}</config-property-setting>\n");
077: sb
078: .append(" <config-property-setting name=\"Password\">{5}</config-property-setting>\n");
079: sb
080: .append(" <workmanager><gbean-link>DefaultWorkManager</gbean-link></workmanager>\n");
081: sb.append(" </resourceadapter-instance>\n");
082: sb.append(" <outbound-resourceadapter>\n");
083: sb.append(" <connection-definition>\n");
084: sb
085: .append(" <connectionfactory-interface>javax.jms.ConnectionFactory</connectionfactory-interface>\n");
086: sb.append(" <connectiondefinition-instance>\n");
087: sb.append(" <name>{6}</name>\n");
088: sb
089: .append(" <implemented-interface>javax.jms.QueueConnectionFactory</implemented-interface>\n");
090: sb
091: .append(" <implemented-interface>javax.jms.TopicConnectionFactory</implemented-interface>\n");
092: sb.append(" <connectionmanager>\n");
093: sb.append(" <xa-transaction>\n");
094: sb.append(" <transaction-caching/>\n");
095: sb.append(" </xa-transaction>\n");
096: sb.append(" <single-pool>\n");
097: sb.append(" <max-size>{7}</max-size>\n");
098: sb
099: .append(" <blocking-timeout-milliseconds>{8}</blocking-timeout-milliseconds>\n");
100: sb.append(" <match-one/>\n");
101: sb.append(" </single-pool>\n");
102: sb.append(" </connectionmanager>\n");
103: sb.append(" </connectiondefinition-instance>\n");
104: sb.append(" </connection-definition>\n");
105: sb.append(" </outbound-resourceadapter>\n");
106: sb.append(" </resourceadapter>\n");
107: sb.append("</connector>\n");
108:
109: return sb.toString();
110: }
111:
112: private void savePlan(File f, Object[] args) {
113: MessageFormat mf = new MessageFormat(PLAN_TEMPLATE);
114: String plan = mf.format(args);
115:
116: try {
117: f.createNewFile();
118: FileOutputStream fos = new FileOutputStream(f);
119: OutputStreamWriter osw = new OutputStreamWriter(fos);
120: Writer out = new BufferedWriter(osw);
121: out.write(plan);
122: out.flush();
123: out.close();
124: osw.close();
125: fos.close();
126: } catch (Exception e) {
127: log.error("Problem creating the plan file", e);
128: }
129: }
130:
131: public void deployPlan(PortletRequest request, Object[] args) {
132: try {
133: File file = File.createTempFile(
134: "console-jms-connector-plan-", ".xml");
135: file.deleteOnExit();
136: savePlan(file, args);
137: if (MODULE_FILE == null) {
138: MODULE_FILE = PortletManager.getCurrentServer(request)
139: .getServerInfo().resolvePath(ACTIVEMQ_RAR);
140: }
141: deployPlan(new File(MODULE_FILE), file);
142: } catch (IOException e) {
143: log.error("Unable to write deployment plan", e);
144: }
145: }
146:
147: public void deployPlan(File moduleFile, File planFile) {
148: try {
149: Kernel kernel = KernelRegistry.getSingleKernel();
150: List list = (List) kernel.invoke(
151: ObjectNameConstants.DEPLOYER_OBJECT_NAME,
152: DEPLOY_METHOD,
153: new Object[] { moduleFile, planFile },
154: DEPLOYER_ARGS);
155: ConfigurationManager configurationManager = ConfigurationUtil
156: .getConfigurationManager(kernel);
157: for (Iterator iterator = list.iterator(); iterator
158: .hasNext();) {
159: Artifact configID = Artifact.create((String) iterator
160: .next());
161: if (!configurationManager.isLoaded(configID)) {
162: configurationManager.loadConfiguration(configID);
163: }
164:
165: configurationManager.startConfiguration(configID);
166: }
167: } catch (DeploymentException e) {
168: StringBuffer buf = new StringBuffer(256);
169: Throwable cause = e;
170: while (cause != null) {
171: buf.append(cause.getMessage());
172: buf.append(LINE_SEP);
173: cause = cause.getCause();
174: }
175: log.error("Problem deploying the ActiveMQ connector: "
176: + buf);
177: } catch (URISyntaxException e) {
178: log.error("Newly installed app has invalid config ID", e);
179: } catch (Exception e) {
180: log.error("Problem creating the datasource", e);
181: }
182: }
183:
184: public List getDependencies(PortletRequest request) {
185: ListableRepository[] repo = PortletManager.getCurrentServer(
186: request).getRepositories();
187: List dependencies = new ArrayList();
188: for (int i = 0; i < repo.length; i++) {
189: ListableRepository repository = repo[i];
190: SortedSet artifacts = repository.list();
191: for (Iterator iterator = artifacts.iterator(); iterator
192: .hasNext();) {
193: Artifact artifact = (Artifact) iterator.next();
194: dependencies.add(artifact.toString());
195: }
196: }
197:
198: return dependencies;
199: }
200: }
|