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