001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * SequencingEngineSUManager.java
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license term
009: */
010: package com.sun.jbi.engine.sequencing;
011:
012: import com.sun.jbi.common.Util;
013: import com.sun.jbi.common.management.ComponentMessageHolder;
014: import com.sun.jbi.common.management.ManagementMessageBuilder;
015: import com.sun.jbi.engine.sequencing.servicelist.ServicelistBean;
016: import com.sun.jbi.engine.sequencing.servicelist.ServicelistReader;
017: import com.sun.jbi.engine.sequencing.util.DeployHelper;
018: import com.sun.jbi.engine.sequencing.util.StringTranslator;
019:
020: import java.util.logging.Logger;
021:
022: import javax.jbi.component.ComponentContext;
023: import javax.jbi.management.DeploymentException;
024:
025: /**
026: * Service unit manager for SE.
027: *
028: * @author Sun Microsystems, Inc.
029: */
030: public class SequencingEngineSUManager implements
031: javax.jbi.component.ServiceUnitManager,
032: SequencingEngineResources {
033: /**
034: * Component Context.
035: */
036: private ComponentContext mContext;
037:
038: /**
039: * Helper class for deployment.
040: */
041: private DeployHelper mHelper;
042:
043: /**
044: * Logger.
045: */
046: private Logger mLog;
047:
048: /**
049: * Deployer messages.
050: */
051: private ManagementMessageBuilder mBuildManagementMessage;
052:
053: /**
054: * Service manager which takes care of starting/stopping services.
055: */
056: private ServiceManager mManager;
057:
058: /**
059: * i18n.
060: */
061: private StringTranslator mTranslator;
062:
063: /**
064: * Creates a new instance of FileBindingSUManager.
065: */
066: public SequencingEngineSUManager() {
067: mLog = SequencingEngineContext.getInstance().getLogger();
068: mTranslator = new StringTranslator();
069: }
070:
071: /**
072: * Returns a list of all application sub-assemblies (ASA's) currently
073: * deployed in to the named component. This method is a convenient
074: * wrapper for the same operation in the DeployerMBean.
075: *
076: * @return array of SU UUID strings.
077: */
078: public String[] getDeployments() {
079: DeploymentRegistry dr = DeploymentRegistry.getInstance();
080:
081: return dr.getAllDeployments();
082: }
083:
084: /**
085: * Sets the component context, and SU manager.
086: *
087: * @param ctx component context.
088: * @param manager su manager.
089: */
090: public void setValues(ComponentContext ctx, ServiceManager manager) {
091: mContext = ctx;
092: mManager = manager;
093: mBuildManagementMessage = Util.createManagementMessageBuilder();
094: }
095:
096: /**
097: * This method is called by the framework when a deployment request comes
098: * for the file binding.
099: *
100: * @param suId Service unit id.
101: * @param suPath Service unit path.
102: *
103: * @return status message.
104: *
105: * @throws javax.jbi.management.DeploymentException deploy exception.
106: * @throws DeploymentException
107: */
108: public String deploy(String suId, String suPath)
109: throws javax.jbi.management.DeploymentException {
110: DeploymentRegistry dr = DeploymentRegistry.getInstance();
111: String retMsg = null;
112:
113: if (dr.getDeploymentStatus(suId)) {
114: mLog.severe(mTranslator.getString(SEQ_DUPLICATE_DEPLOYMENT,
115: suId));
116: throw new DeploymentException(createExceptionMessage(
117: mContext.getComponentName(), "deploy", "FAILED",
118: "SEQ_30001", suId, mTranslator.getString(
119: SEQ_DUPLICATE_DEPLOYMENT, suId), null));
120: }
121:
122: try {
123: initializeDeployment(suId, suPath);
124: } catch (DeploymentException de) {
125: throw de;
126: } catch (Exception e) {
127: throw new DeploymentException(createExceptionMessage(
128: mContext.getComponentName(), "deploy", "FAILED",
129: "SEQ_30009", suId, mTranslator.getString(
130: SEQ_XML_STRING_CREATION_FAILED, suId), e));
131: }
132:
133: try {
134: ComponentMessageHolder compMsgHolder = new ComponentMessageHolder(
135: "STATUS_MSG");
136: compMsgHolder.setComponentName(mContext.getComponentName());
137: compMsgHolder.setTaskName("deploy");
138: compMsgHolder.setTaskResult("SUCCESS");
139:
140: retMsg = mBuildManagementMessage
141: .buildComponentMessage(compMsgHolder);
142: } catch (Exception e) {
143: mLog.severe(mTranslator
144: .getString(SEQ_XML_STRING_CREATION_FAILED));
145: mLog.severe(e.getMessage());
146: }
147:
148: return retMsg;
149: }
150:
151: /**
152: * This method is called by the framework when the deployment has to be \
153: * initialised , just before starting it.
154: *
155: * @param suId service unit id.
156: * @param suPath service unit path.
157: *
158: * @throws javax.jbi.management.DeploymentException DeploymentException
159: * @throws DeploymentException
160: */
161: public void init(String suId, String suPath)
162: throws javax.jbi.management.DeploymentException {
163: DeploymentRegistry dr = DeploymentRegistry.getInstance();
164:
165: if (!dr.getDeploymentStatus(suId)) {
166: try {
167: initializeDeployment(suId, suPath);
168: } catch (DeploymentException de) {
169: throw de;
170: } catch (Exception e) {
171: throw new DeploymentException(createExceptionMessage(
172: mContext.getComponentName(), "deploy",
173: "FAILED", "SEQ_30009", suId, mTranslator
174: .getString(
175: SEQ_XML_STRING_CREATION_FAILED,
176: suId), e));
177: }
178: }
179:
180: }
181:
182: /**
183: * Shuts down the service unit.
184: *
185: * @param str su id.
186: *
187: * @throws javax.jbi.management.DeploymentException depl exception
188: */
189: public void shutDown(String str)
190: throws javax.jbi.management.DeploymentException {
191: }
192:
193: /**
194: * Starts an SU.
195: *
196: * @param suId su id.
197: *
198: * @throws javax.jbi.management.DeploymentException DeploymentException
199: * @throws DeploymentException
200: */
201: public void start(String suId)
202: throws javax.jbi.management.DeploymentException {
203:
204: try {
205: mManager.startDeployment(suId);
206: } catch (Exception e) {
207: e.printStackTrace();
208: throw new DeploymentException(createExceptionMessage(
209: mContext.getComponentName(), "deploy", "FAILED",
210: "SEQ_30009", suId, mTranslator.getString(
211: SEQ_START_DEPLOYMENT_FAILED, suId), e));
212: }
213:
214: if (!mManager.isValid()) {
215: mLog.severe(mTranslator.getString(
216: SEQ_START_DEPLOYMENT_FAILED, suId));
217: mLog.severe(mManager.getError());
218: throw new DeploymentException(createExceptionMessage(
219: mContext.getComponentName(), "deploy", "FAILED",
220: "SEQ_30009", suId, mManager.getError(), null));
221: }
222:
223: }
224:
225: /**
226: * Stops an SU.
227: *
228: * @param suId su id.
229: *
230: * @throws javax.jbi.management.DeploymentException DeploymentException
231: * @throws DeploymentException
232: */
233: public void stop(String suId)
234: throws javax.jbi.management.DeploymentException {
235: mManager.stopDeployment(suId);
236:
237: if (!mManager.isValid()) {
238: throw new DeploymentException(createExceptionMessage(
239: mContext.getComponentName(), "deploy", "FAILED",
240: "SEQ_30009", suId, mManager.getError(), null));
241: }
242: }
243:
244: /**
245: * Undeploys an SU.
246: *
247: * @param suId su id.
248: * @param suPath su path.
249: *
250: * @return management message string.
251: *
252: * @throws javax.jbi.management.DeploymentException DeploymentException
253: * @throws DeploymentException
254: */
255: public String undeploy(String suId, String suPath)
256: throws javax.jbi.management.DeploymentException {
257: DeploymentRegistry dr = DeploymentRegistry.getInstance();
258: ServicelistBean slb = dr.getService(suId);
259:
260: if (slb == null) {
261: throw new DeploymentException(createExceptionMessage(
262: mContext.getComponentName(), "undeploy", "FAILED",
263: "SEQ_30006", "", "SU not deployed", null));
264: }
265:
266: try {
267: dr.deregisterService(slb.getNamespace()
268: + slb.getServicename() + slb.getEndpointName()
269: + slb.getOperation());
270: } catch (Exception e) {
271: throw new DeploymentException(createExceptionMessage(
272: mContext.getComponentName(), "undeploy", "FAILED",
273: "SEQ_30006", "", e.getMessage(), e));
274: }
275:
276: String retMsg = null;
277:
278: try {
279: ComponentMessageHolder compMsgHolder = new ComponentMessageHolder(
280: "STATUS_MSG");
281: compMsgHolder.setComponentName(mContext.getComponentName());
282: compMsgHolder.setTaskName("undeploy");
283: compMsgHolder.setTaskResult("SUCCESS");
284:
285: retMsg = mBuildManagementMessage
286: .buildComponentMessage(compMsgHolder);
287: } catch (Exception e) {
288: mLog.severe(mTranslator
289: .getString(SEQ_XML_STRING_CREATION_FAILED));
290: mLog.severe(e.getMessage());
291: }
292:
293: return retMsg;
294: }
295:
296: /**
297: * helper method to create XML exception string.
298: *
299: * @param compid Component id.
300: * @param oper operation like deploy or undeploy
301: * @param status success r failure
302: * @param loctoken some failure string token like SEQ_300001
303: * @param locparam parameters for error message.
304: * @param locmessage error message.
305: * @param exObj stack trace of exception.
306: *
307: * @return XML string.
308: */
309: private String createExceptionMessage(String compid, String oper,
310: String status, String loctoken, String locparam,
311: String locmessage, Throwable exObj) {
312: String[] locParams = new String[1];
313: locParams[0] = locparam;
314:
315: ComponentMessageHolder msgMap = new ComponentMessageHolder(
316: "EXCEPTION_MSG");
317:
318: msgMap.setComponentName(compid);
319: msgMap.setTaskName(oper);
320: msgMap.setTaskResult(status);
321: msgMap.setLocToken(1, loctoken);
322: msgMap.setLocParam(1, locParams);
323: msgMap.setLocMessage(1, locmessage);
324: msgMap.setExceptionObject(exObj);
325:
326: String retMsg = null;
327:
328: try {
329: retMsg = mBuildManagementMessage
330: .buildComponentMessage(msgMap);
331: } catch (Exception e) {
332: mLog.severe(mTranslator
333: .getString(SEQ_XML_STRING_CREATION_FAILED));
334: mLog.severe(e.getMessage());
335: }
336:
337: return retMsg;
338: }
339:
340: /**
341: * initalises deployment.
342: *
343: * @param suId su id.
344: * @param suPath su path.
345: *
346: * @throws javax.jbi.management.DeploymentException DeploymentException
347: * @throws DeploymentException
348: */
349: private void initializeDeployment(String suId, String suPath)
350: throws javax.jbi.management.DeploymentException {
351: mHelper = new DeployHelper(suId, suPath); // , mResolver);
352: mHelper.doDeploy(true);
353:
354: if (!mHelper.isValid()) {
355: mLog.severe(mHelper.getError());
356:
357: Exception e = mHelper.getException();
358:
359: if (e != null) {
360: throw new DeploymentException(createExceptionMessage(
361: mContext.getComponentName(), "deploy",
362: "FAILED", "SEQ_30005", "", mHelper.getError(),
363: e));
364: } else {
365: throw new DeploymentException(createExceptionMessage(
366: mContext.getComponentName(), "deploy",
367: "FAILED", "SEQ_30005", "", mHelper.getError(),
368: null));
369: }
370: }
371: }
372:
373: /**
374: * Starts a deployment.
375: *
376: * @param suId su id.
377: *
378: * @return management message string.
379: */
380: private String startDeployment(String suId) {
381: String retMsg = null;
382: mManager.startDeployment(suId);
383:
384: try {
385: ComponentMessageHolder compMsgHolder = new ComponentMessageHolder(
386: "STATUS_MSG");
387: compMsgHolder.setComponentName(mContext.getComponentName());
388: compMsgHolder.setTaskName("deploy");
389: compMsgHolder.setTaskResult("SUCCESS");
390:
391: if (!mManager.isValid()) {
392: compMsgHolder.setTaskResult("FAILED");
393: compMsgHolder.setLocMessage(1, mManager.getError());
394: } else if (!mManager.getWarning().trim().equals("")) {
395: compMsgHolder.setStatusMessageType("WARNING");
396: compMsgHolder.setLocMessage(1, mManager.getWarning());
397: }
398:
399: retMsg = mBuildManagementMessage
400: .buildComponentMessage(compMsgHolder);
401: } catch (Exception e) {
402: mLog.severe(mTranslator
403: .getString(SEQ_XML_STRING_CREATION_FAILED));
404: mLog.severe(e.getMessage());
405: }
406:
407: return retMsg;
408: }
409: }
|