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