001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JMSBindingSUManager.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.jms.deploy;
030:
031: import com.sun.jbi.StringTranslator;
032:
033: import com.sun.jbi.binding.jms.EndpointBean;
034: import com.sun.jbi.binding.jms.EndpointManager;
035: import com.sun.jbi.binding.jms.EndpointStatus;
036: import com.sun.jbi.binding.jms.JMSBindingContext;
037: import com.sun.jbi.binding.jms.JMSBindingResolver;
038: import com.sun.jbi.binding.jms.JMSBindingResources;
039:
040: import com.sun.jbi.common.Util;
041:
042: import com.sun.jbi.common.management.ComponentMessageHolder;
043: import com.sun.jbi.common.management.ManagementMessageBuilder;
044:
045: import java.util.Collection;
046: import java.util.Iterator;
047:
048: import java.util.logging.Logger;
049:
050: import javax.jbi.component.ComponentContext;
051:
052: import javax.jbi.management.DeploymentException;
053:
054: /**
055: * This class handles deployments to the JMS binding.
056: *
057: * @author Sun Microsystems Inc.
058: */
059: public class JMSBindingSUManager implements
060: javax.jbi.component.ServiceUnitManager, JMSBindingResources {
061: /**
062: * Component context.
063: */
064: private ComponentContext mContext;
065: /**
066: * Deploy helper.
067: */
068: private DeployHelper mHelper;
069: /**
070: * Endpoint Manager.
071: */
072: private EndpointManager mManager;
073: /**
074: * Resolver object.
075: */
076: private JMSBindingResolver mResolver;
077: /**
078: * Logger object.
079: */
080: private Logger mLogger;
081: /**
082: * Deployer messages.
083: */
084: private ManagementMessageBuilder mBuildManagementMessage;
085: /**
086: * String translator.
087: */
088: private StringTranslator mStringTranslator;
089:
090: /**
091: * Creates a new JMSBindingSUManager object.
092: */
093: public JMSBindingSUManager() {
094: mLogger = JMSBindingContext.getInstance().getLogger();
095: }
096:
097: /**
098: * Sets the translator.
099: */
100: private void setTranslator() {
101: mStringTranslator = JMSBindingContext.getInstance()
102: .getStringTranslator();
103: }
104:
105: /**
106: * Returns a list of all Service units (SUs) currently
107: * deployed in to the named component. This method is a convenient
108: * wrapper for the same operation in the DeployerMBean.
109: *
110: * @return array of SU UUID strings.
111: */
112: public String[] getDeployments() {
113: EndpointRegistry dr = JMSBindingContext.getInstance()
114: .getRegistry();
115:
116: return dr.getAllDeployments();
117: }
118:
119: /**
120: * Sets values for JMSBindingSUManager.
121: *
122: * @param ctx component context.
123: * @param manager endpoint manager.
124: * @param reslv resolver.
125: */
126: public void setValues(ComponentContext ctx,
127: EndpointManager manager, JMSBindingResolver reslv) {
128: mContext = ctx;
129: mManager = manager;
130: mResolver = reslv;
131: mBuildManagementMessage = Util.createManagementMessageBuilder();
132: }
133:
134: /**
135: * This method is called by the framework when a deployment request comes
136: * for the file binding.
137: *
138: * @param suId service unit ID.
139: * @param suPath path to the su.
140: *
141: * @return deploy result.
142: *
143: * @throws DeploymentException deployment exception.
144: */
145: public String deploy(String suId, String suPath)
146: throws javax.jbi.management.DeploymentException {
147: setTranslator();
148:
149: EndpointRegistry dr = JMSBindingContext.getInstance()
150: .getRegistry();
151: String retMsg = null;
152: if ((dr.getEndpoints(suId) != null)
153: && (dr.getEndpoints(suId).size() != 0)) {
154: mLogger.severe(mStringTranslator.getString(
155: JMS_DUPLICATE_DEPLOYMENT, suId));
156: throw new DeploymentException(
157: createExceptionMessage(mContext.getComponentName(),
158: "deploy", "FAILED", "JMS_30001", suId,
159: mStringTranslator.getString(
160: JMS_DUPLICATE_DEPLOYMENT, suId),
161: new DeploymentException(mStringTranslator
162: .getString(
163: JMS_DUPLICATE_DEPLOYMENT,
164: suId))));
165: }
166:
167: /**
168: * You have to call initialize deployments here because, thats where
169: * the schema check and data check are made, you want to fail the
170: * deployment if the artifact fails validation. The same stuff is done
171: * in init also during component starts , but if you dont do it here u
172: * cannot fail deployments because of invalid data.
173: */
174: String warning = null;
175: try {
176: warning = initializeDeployment(suId, suPath);
177:
178: /*
179: * Do not start deployments here.
180: * The Deployment service will call init and start separately
181: * after finidhing the deploy method.
182: */
183: } catch (DeploymentException de) {
184: throw de;
185: } catch (Exception e) {
186: throw new DeploymentException(createExceptionMessage(
187: mContext.getComponentName(), "deploy", "FAILED",
188: "JMS_30009", suId, mStringTranslator.getString(
189: JMS_XML_STRING_CREATION_FAILED, suId), e));
190: }
191:
192: try {
193: ComponentMessageHolder compMsgHolder = new ComponentMessageHolder(
194: "STATUS_MSG");
195: compMsgHolder.setComponentName(mContext.getComponentName());
196: compMsgHolder.setTaskName("deploy");
197: compMsgHolder.setTaskResult("SUCCESS");
198: if (warning != null) {
199: if (!warning.trim().equals("")) {
200: String[] locParams = new String[1];
201: locParams[0] = suId;
202: compMsgHolder.setStatusMessageType("WARNING");
203: compMsgHolder.setLocToken(1, "JMS_30010");
204: compMsgHolder.setLocParam(1, locParams);
205: compMsgHolder.setLocMessage(1, warning);
206: }
207: }
208: retMsg = mBuildManagementMessage
209: .buildComponentMessage(compMsgHolder);
210: } catch (Exception e) {
211: mLogger.severe(mStringTranslator
212: .getString(JMS_XML_STRING_CREATION_FAILED));
213: mLogger.severe(e.getMessage());
214: }
215: updateState(suId, EndpointStatus.DEPLOYED);
216: return retMsg;
217: }
218:
219: /**
220: * This method is called by the framework when the deployment has to be \
221: * initialised , just before starting it.
222: *
223: * @param suId service unit ID.
224: * @param suPath service unit path.
225: * @throws DeploymentException deployment exception.
226: */
227: public void init(String suId, String suPath)
228: throws javax.jbi.management.DeploymentException {
229: mLogger.info("**INITIALISING DEPLOYMENT** " + suId);
230:
231: /*
232: * This may be called as soon as the deployment has hapened
233: * or when the component starts
234: * When the component starts up, we have to redeploy this SU
235: * internally. This is how simple bindings and engines work. In case
236: * of grown up components they might have their own ways of tracking
237: * SU deployments and may not have to initialize deployments
238: * everytime the component starts.
239: */
240: EndpointRegistry dr = JMSBindingContext.getInstance()
241: .getRegistry();
242:
243: if ((dr.getEndpoints(suId) != null)
244: && (dr.getEndpoints(suId).size() == 0)) {
245: String warning = null;
246: try {
247: warning = initializeDeployment(suId, suPath);
248: } catch (DeploymentException de) {
249: de.printStackTrace();
250: throw de;
251: } catch (Exception e) {
252: e.printStackTrace();
253: throw new DeploymentException(createExceptionMessage(
254: mContext.getComponentName(), "deploy",
255: "FAILED", "JMS_30009", suId, mStringTranslator
256: .getString(
257: JMS_XML_STRING_CREATION_FAILED,
258: suId), e));
259: }
260: if (warning != null) {
261: if (!warning.trim().equals("")) {
262: mLogger.warning(warning);
263: }
264: }
265: }
266: updateState(suId, EndpointStatus.INIT);
267: mLogger.info("**INITIALISED DEPLOYMENT** " + suId);
268: }
269:
270: /**
271: * Shut down the service unit.
272: *
273: * @param suId service unit ID.
274: *
275: * @throws javax.jbi.management.DeploymentException deployment exception.
276: */
277: public void shutDown(String suId)
278: throws javax.jbi.management.DeploymentException {
279: updateState(suId, EndpointStatus.SHUTDOWN);
280: }
281:
282: /**
283: * Starts the SU.
284: *
285: * @param suId Service unit ID.
286: *
287: * @throws javax.jbi.management.DeploymentException deployment exception.
288: */
289: public void start(String suId)
290: throws javax.jbi.management.DeploymentException {
291: setTranslator();
292: mLogger.fine("**STARTING DEPLOYMENT** " + suId);
293:
294: try {
295: mManager.startDeployment(suId);
296: } catch (Exception e) {
297: throw new DeploymentException(createExceptionMessage(
298: mContext.getComponentName(), "deploy", "FAILED",
299: "JMS_30009", suId, mStringTranslator.getString(
300: JMS_XML_STRING_CREATION_FAILED, suId), e));
301: }
302:
303: if (!mManager.isValid()) {
304: throw new DeploymentException(createExceptionMessage(
305: mContext.getComponentName(), "deploy", "FAILED",
306: "JMS_30009", suId, mManager.getError(), null));
307: }
308: updateState(suId, EndpointStatus.STARTED);
309: mLogger.info("**STARTED DEPLOYMENT** " + suId);
310: }
311:
312: /**
313: * Stops the SU.
314: *
315: * @param suId Service unit.
316: *
317: * @throws javax.jbi.management.DeploymentException deployment exception.
318: */
319: public void stop(String suId)
320: throws javax.jbi.management.DeploymentException {
321: mManager.stopDeployment(suId);
322:
323: if (!mManager.isValid()) {
324: throw new DeploymentException(createExceptionMessage(
325: mContext.getComponentName(), "deploy", "FAILED",
326: "JMS_30009", suId, mManager.getError(), null));
327: }
328: updateState(suId, EndpointStatus.STOPPED);
329: }
330:
331: /**
332: * Undeploys the Service unit.
333: *
334: * @param suId service unit ID.
335: * @param suPath service unit path.
336: *
337: * @return deploy result.
338: *
339: * @throws javax.jbi.management.DeploymentException deployment exception.
340: */
341: public String undeploy(String suId, String suPath)
342: throws javax.jbi.management.DeploymentException {
343: String retMsg = null;
344: setTranslator();
345: EndpointRegistry dr = JMSBindingContext.getInstance()
346: .getRegistry();
347: try {
348: Collection l = dr.getEndpoints(suId);
349: Iterator iter = l.iterator();
350:
351: while (iter.hasNext()) {
352: EndpointBean eb = (EndpointBean) iter.next();
353: String epname = null;
354: epname = eb.getUniqueName();
355: dr.deregisterEndpoint(epname);
356: dr.persist();
357: }
358: } catch (Exception e) {
359: e.printStackTrace();
360: throw new DeploymentException(createExceptionMessage(
361: mContext.getComponentName(), "undeploy", "FAILED",
362: "JMS_30006", "", e.getMessage(), e));
363: }
364: try {
365: ComponentMessageHolder compMsgHolder = new ComponentMessageHolder(
366: "STATUS_MSG");
367: compMsgHolder.setComponentName(mContext.getComponentName());
368: compMsgHolder.setTaskName("undeploy");
369: compMsgHolder.setTaskResult("SUCCESS");
370:
371: retMsg = mBuildManagementMessage
372: .buildComponentMessage(compMsgHolder);
373: } catch (Exception e) {
374: mLogger.severe(mStringTranslator
375: .getString(JMS_XML_STRING_CREATION_FAILED));
376: mLogger.severe(e.getMessage());
377: }
378: return retMsg;
379: }
380:
381: /**
382: * helper method to create XML exception string.
383: *
384: * @param compid Component id.
385: * @param oper operation like deploy or undeploy
386: * @param status success r failure
387: * @param loctoken some failure string token like SEQ_300001
388: * @param locparam parameters for error message.
389: * @param locmessage error message.
390: * @param exObj stack trace of exception.
391: *
392: * @return XML string.
393: */
394: private String createExceptionMessage(String compid, String oper,
395: String status, String loctoken, String locparam,
396: String locmessage, Throwable exObj) {
397: String[] locParams = new String[1];
398: locParams[0] = locparam;
399: setTranslator();
400: ComponentMessageHolder msgMap = new ComponentMessageHolder(
401: "EXCEPTION_MSG");
402:
403: msgMap.setComponentName(compid);
404: msgMap.setTaskName(oper);
405: msgMap.setTaskResult(status);
406: msgMap.setLocToken(1, loctoken);
407: msgMap.setLocParam(1, locParams);
408: msgMap.setLocMessage(1, locmessage);
409: msgMap.setExceptionObject(exObj);
410:
411: String retMsg = null;
412:
413: try {
414: retMsg = mBuildManagementMessage
415: .buildComponentMessage(msgMap);
416: } catch (Exception e) {
417: mLogger.severe(mStringTranslator
418: .getString(JMS_XML_STRING_CREATION_FAILED));
419: mLogger.severe(e.getMessage());
420: }
421:
422: return retMsg;
423: }
424:
425: /**
426: * Initializes the deployments.
427: *
428: * @param suId Service unit id.
429: * @param suPath Service unit path.
430: *
431: * @return string warning if any.
432: * @throws javax.jbi.management.DeploymentException deployment exception.
433: */
434: private String initializeDeployment(String suId, String suPath)
435: throws javax.jbi.management.DeploymentException {
436: mHelper = new DeployHelper(suId, suPath, mContext, mResolver);
437: mHelper.doDeploy(true);
438:
439: if (!mHelper.isValid()) {
440: mLogger.severe(mHelper.getError());
441:
442: Exception e = mHelper.getException();
443:
444: if (e != null) {
445: throw new DeploymentException(createExceptionMessage(
446: mContext.getComponentName(), "deploy",
447: "FAILED", "JMS_30005", "", mHelper.getError(),
448: e));
449: } else {
450: throw new DeploymentException(createExceptionMessage(
451: mContext.getComponentName(), "deploy",
452: "FAILED", "JMS_30005", "", mHelper.getError(),
453: null));
454: }
455: }
456: return mHelper.getWarning();
457: }
458:
459: /**
460: * Updates the endpoint states for all endpoints in the deployment.
461: *
462: * @param suId Service unit ID.
463: * @param state endpoint state.
464: */
465:
466: private void updateState(String suId, EndpointStatus state) {
467: EndpointRegistry dr = JMSBindingContext.getInstance()
468: .getRegistry();
469: try {
470: Collection l = dr.getEndpoints(suId);
471: Iterator iter = l.iterator();
472: while (iter.hasNext()) {
473: EndpointBean eb = (EndpointBean) iter.next();
474: if (eb != null) {
475: eb.setStatus(state);
476: }
477: }
478: } catch (Exception e) {
479: e.printStackTrace();
480: }
481:
482: }
483: }
|