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: * @(#)EndpointManager.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.FileBindingContext;
032: import com.sun.jbi.binding.file.util.ConfigData;
033: import com.sun.jbi.binding.file.util.StringTranslator;
034: import com.sun.jbi.binding.file.util.UtilBase;
035:
036: import java.io.File;
037:
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.logging.Logger;
041:
042: import javax.jbi.JBIException;
043: import javax.jbi.component.ComponentContext;
044: import javax.jbi.servicedesc.ServiceEndpoint;
045:
046: import javax.xml.namespace.QName;
047:
048: /**
049: * Manages the stopping and starting of endpoints.
050: *
051: * @author Sun Microsystems, Inc.
052: */
053: public class EndpointManager extends UtilBase implements
054: FileBindingResources {
055: /**
056: * Sleep time for thread.
057: */
058: private static final long SLEEP_TIME = 50;
059:
060: /**
061: * Component Context.
062: */
063: private ComponentContext mContext;
064:
065: /**
066: * Deployment Registry.
067: */
068: private DeploymentRegistry mRegistry;
069:
070: /**
071: * Logger object.
072: */
073: private Logger mLog;
074:
075: /**
076: * Helper for i18n.
077: */
078: private StringTranslator mStringTranslator;
079:
080: /**
081: * Creates a new EndpointManager object.
082: */
083: public EndpointManager() {
084: mRegistry = DeploymentRegistry.getInstance();
085: mLog = FileBindingContext.getInstance().getLogger();
086: mStringTranslator = new StringTranslator();
087: mContext = FileBindingContext.getInstance().getContext();
088: }
089:
090: /**
091: * Checks the endpoint attributes.
092: *
093: * @param eb bean
094: */
095: public void docheck(EndpointBean eb) {
096: if ((eb.getValue(ConfigData.OUTPUTDIR).trim().equals(""))) {
097: mLog.severe(mStringTranslator.getString(
098: FBC_OUTPUTFOLDER_NOTEXIST, eb
099: .getValue(ConfigData.OUTPUTDIR)));
100: mLog.severe(mStringTranslator.getString(
101: FBC_CANNOT_ACTIVATE_ENDPOINT, eb.getUniqueName()));
102: setError(mStringTranslator.getString(
103: FBC_CANNOT_ACTIVATE_ENDPOINT, eb.getUniqueName()));
104: setError(mStringTranslator.getString(
105: FBC_OUTPUTFOLDER_NOTEXIST, eb
106: .getValue(ConfigData.OUTPUTDIR)));
107:
108: return;
109: }
110:
111: if (eb.getRole() == ConfigData.CONSUMER) {
112: if ((eb.getValue(ConfigData.PROCESSEDDIR).trim().equals(""))) {
113: mLog.severe(mStringTranslator.getString(
114: FBC_PROCESSEDFOLDER_NOTEXIST, eb
115: .getValue(ConfigData.PROCESSEDDIR)));
116: mLog.severe(mStringTranslator.getString(
117: FBC_CANNOT_ACTIVATE_ENDPOINT, eb
118: .getUniqueName()));
119: setError(mStringTranslator.getString(
120: FBC_PROCESSEDFOLDER_NOTEXIST, eb
121: .getValue(ConfigData.PROCESSEDDIR)));
122: setError(mStringTranslator.getString(
123: FBC_CANNOT_ACTIVATE_ENDPOINT, eb
124: .getUniqueName()));
125:
126: return;
127: }
128:
129: if ((eb.getValue(ConfigData.INPUTDIR).trim().equals(""))) {
130: mLog.severe(mStringTranslator.getString(
131: FBC_INPUTFOLDER_NOTEXIST, eb
132: .getValue(ConfigData.INPUTDIR)));
133: mLog.severe(mStringTranslator.getString(
134: FBC_CANNOT_ACTIVATE_ENDPOINT, eb
135: .getUniqueName()));
136: setError(mStringTranslator.getString(
137: FBC_INPUTFOLDER_NOTEXIST, eb
138: .getValue(ConfigData.INPUTDIR)));
139: setError(mStringTranslator.getString(
140: FBC_CANNOT_ACTIVATE_ENDPOINT, eb
141: .getUniqueName()));
142:
143: return;
144: }
145:
146: File inputdir = new File(eb.getValue(ConfigData.INPUTDIR));
147:
148: if (!inputdir.exists()) {
149: mLog.info(mStringTranslator.getString(
150: FBC_INPUTFOLDER_NOTEXIST, inputdir
151: .getAbsolutePath()));
152:
153: if (inputdir.mkdirs()) {
154: mLog.info(mStringTranslator.getString(
155: FBC_CREATED_FOLDER, inputdir
156: .getAbsolutePath()));
157: setWarning(mStringTranslator.getString(
158: FBC_CREATED_FOLDER, inputdir
159: .getAbsolutePath()));
160: } else {
161: mLog.info(mStringTranslator.getString(
162: FBC_CANNOT_CREATE_FOLDER, inputdir
163: .getAbsolutePath()));
164: setError(mStringTranslator.getString(
165: FBC_CANNOT_CREATE_FOLDER, inputdir
166: .getAbsolutePath()));
167:
168: return;
169: }
170: }
171:
172: if ((!inputdir.isDirectory()) || (!inputdir.canRead())) {
173: mLog.severe(mStringTranslator.getString(
174: FBC_INPUTFOLDER_NOTEXIST, inputdir
175: .getAbsolutePath()));
176: mLog.severe(mStringTranslator.getString(
177: FBC_CANNOT_ACTIVATE_ENDPOINT, eb
178: .getUniqueName()));
179: setError(mStringTranslator.getString(
180: FBC_INPUTFOLDER_NOTEXIST, inputdir
181: .getAbsolutePath()));
182: setError(mStringTranslator.getString(
183: FBC_CANNOT_ACTIVATE_ENDPOINT, eb
184: .getUniqueName()));
185:
186: return;
187: }
188:
189: for (int i = 0; i < eb.getOperationsCount(); i++) {
190: QName oper = eb.getOperationQName(i);
191: File f = new File(inputdir.getAbsolutePath()
192: + File.separatorChar + oper.getLocalPart());
193:
194: if (!f.exists()) {
195: mLog.info(mStringTranslator.getString(
196: FBC_INPUTFOLDER_NOTEXIST, inputdir
197: .getAbsolutePath()));
198:
199: if (f.mkdirs()) {
200: mLog.info(mStringTranslator.getString(
201: FBC_CREATED_FOLDER, inputdir
202: .getAbsolutePath()));
203: setWarning(mStringTranslator.getString(
204: FBC_CREATED_FOLDER, inputdir
205: .getAbsolutePath()));
206: } else {
207: mLog.info(mStringTranslator.getString(
208: FBC_CANNOT_CREATE_FOLDER, inputdir
209: .getAbsolutePath()));
210: setWarning(mStringTranslator.getString(
211: FBC_CANNOT_CREATE_FOLDER, inputdir
212: .getAbsolutePath()));
213:
214: return;
215: }
216: }
217: }
218:
219: File processedFolder = new File(eb
220: .getValue(ConfigData.PROCESSEDDIR));
221:
222: if (!processedFolder.exists()) {
223: mLog.info(mStringTranslator.getString(
224: FBC_PROCESSEDFOLDER_NOTEXIST, processedFolder
225: .getAbsolutePath()));
226:
227: if (processedFolder.mkdirs()) {
228: mLog.info(mStringTranslator.getString(
229: FBC_CREATED_FOLDER, processedFolder
230: .getAbsolutePath()));
231: setWarning(mStringTranslator.getString(
232: FBC_CREATED_FOLDER, processedFolder
233: .getAbsolutePath()));
234: } else {
235: mLog.info(mStringTranslator.getString(
236: FBC_CANNOT_CREATE_FOLDER, processedFolder
237: .getAbsolutePath()));
238: setError(mStringTranslator.getString(
239: FBC_CANNOT_CREATE_FOLDER, processedFolder
240: .getAbsolutePath()));
241:
242: return;
243: }
244: }
245:
246: if (!processedFolder.canWrite()) {
247: mLog.warning(mStringTranslator.getString(
248: FBC_NO_WRITE_PERMISSION, processedFolder
249: .getAbsolutePath()));
250: setError(mStringTranslator.getString(
251: FBC_NO_WRITE_PERMISSION, processedFolder
252: .getAbsolutePath()));
253: }
254: }
255: }
256:
257: /**
258: * Checks the endpoint attributes.
259: *
260: * @param eb bean
261: */
262: public void docheck2(EndpointBean eb) {
263: File writeFolder = new File(eb.getValue(ConfigData.OUTPUTDIR));
264:
265: if (!writeFolder.exists()) {
266: mLog.info(mStringTranslator.getString(
267: FBC_OUTPUTFOLDER_NOTEXIST, writeFolder
268: .getAbsolutePath()));
269:
270: if (writeFolder.mkdirs()) {
271: mLog.info(mStringTranslator.getString(
272: FBC_CREATED_FOLDER, writeFolder
273: .getAbsolutePath()));
274: setWarning(mStringTranslator.getString(
275: FBC_CREATED_FOLDER, writeFolder
276: .getAbsolutePath()));
277: } else {
278: mLog.severe(mStringTranslator.getString(
279: FBC_CANNOT_CREATE_FOLDER, writeFolder
280: .getAbsolutePath()));
281: setError(mStringTranslator.getString(
282: FBC_CANNOT_CREATE_FOLDER, writeFolder
283: .getAbsolutePath()));
284:
285: return;
286: }
287: }
288:
289: if (!writeFolder.canWrite()) {
290: mLog.warning(mStringTranslator.getString(
291: FBC_NO_WRITE_PERMISSION, writeFolder
292: .getAbsolutePath()));
293: }
294:
295: if (eb.getRole() == ConfigData.CONSUMER) {
296: if ((eb.getValue(ConfigData.SERVICE_LOCALNAME).trim()
297: .equals(""))
298: && (eb.getValue(ConfigData.ENDPOINTNAME).trim()
299: .equals(""))
300: && (eb.getValue(ConfigData.INTERFACE_LOCALNAME)
301: .trim().equals(""))) {
302: mLog.severe(mStringTranslator
303: .getString(FBC_INVALID_ENDPOINT_INFO, eb
304: .getDeploymentId()));
305: setError(mStringTranslator
306: .getString(FBC_INVALID_ENDPOINT_INFO, eb
307: .getDeploymentId()));
308:
309: return;
310: }
311: } else if (eb.getRole() == ConfigData.PROVIDER) {
312: if ((eb.getValue(ConfigData.SERVICE_LOCALNAME).trim()
313: .equals(""))
314: || (eb.getValue(ConfigData.ENDPOINTNAME).trim()
315: .equals(""))
316: || (eb.getValue(ConfigData.INTERFACE_LOCALNAME)
317: .trim().equals(""))) {
318: mLog.severe(mStringTranslator.getString(
319: FBC_INVALID_PROVIDER_ENDPOINT_INFO, eb
320: .getDeploymentId()));
321: setError(mStringTranslator.getString(
322: FBC_INVALID_PROVIDER_ENDPOINT_INFO, eb
323: .getDeploymentId()));
324:
325: return;
326: }
327: }
328: }
329:
330: /**
331: * Starts a deployment.
332: *
333: * @param suid service unit id.
334: */
335: public void startDeployment(String suid) {
336: mLog.info(mStringTranslator.getString(FBC_START_DEPLOYMENT,
337: suid));
338: super .clear();
339:
340: List l = mRegistry.getEndpoints(suid);
341: Iterator iter = l.iterator();
342:
343: while (iter.hasNext()) {
344: EndpointBean eb = (EndpointBean) iter.next();
345:
346: if (eb == null) {
347: mLog.severe(mStringTranslator.getString(
348: FBC_START_DEPLOYMENT_FAILED, suid));
349: mLog.severe(mStringTranslator.getString(
350: FBC_START_DEPLOYMENT_FAILED_BEANNULL, suid));
351: setWarning(mStringTranslator.getString(
352: FBC_START_DEPLOYMENT_FAILED_BEANNULL, suid));
353:
354: continue;
355: }
356:
357: docheck(eb);
358: docheck2(eb);
359:
360: if (!isValid()) {
361: setWarning(mStringTranslator.getString(
362: FBC_START_ENDPOINT_FAILED, eb.getUniqueName(),
363: suid));
364:
365: return;
366: }
367:
368: if (eb.getRole() == ConfigData.PROVIDER) {
369: if (!startOutboundEndpoint(eb)) {
370: setWarning(mStringTranslator.getString(
371: FBC_START_ENDPOINT_FAILED, eb
372: .getUniqueName(), suid));
373:
374: continue;
375: }
376: } else if (eb.getRole() == ConfigData.CONSUMER) {
377: if (!startInboundEndpoint(eb)) {
378: setWarning(mStringTranslator.getString(
379: FBC_START_ENDPOINT_FAILED, eb
380: .getUniqueName(), suid));
381: stopInboundEndpoint(eb);
382:
383: continue;
384: }
385: }
386: }
387:
388: mLog.info(mStringTranslator.getString(
389: FBC_START_DEPLOYMENT_SUCCESS, suid));
390: }
391:
392: /**
393: * Starts a consumer endpoint.
394: *
395: * @param eb endpoint bean
396: *
397: * @return true if started successfuly.
398: */
399: public boolean startInboundEndpoint(EndpointBean eb) {
400: try {
401: FileThreads ft = new FileThreads(eb);
402: Thread t = new Thread(ft);
403: eb.setFileThread(ft);
404: eb.setReceiverThread(t);
405: t.start();
406: } catch (Exception e) {
407: mLog.severe(mStringTranslator.getString(
408: FBC_START_ENDPOINT_FAILED, eb.getUniqueName()));
409: mLog.severe(e.getMessage());
410: setWarning(e.getMessage());
411:
412: return false;
413: }
414:
415: return true;
416: }
417:
418: /**
419: * Starts a provider endpoint.
420: *
421: * @param eb bean.
422: *
423: * @return true if success.
424: */
425: public boolean startOutboundEndpoint(EndpointBean eb) {
426: ServiceEndpoint ref = null;
427:
428: try {
429: ref = mContext.activateEndpoint(new QName(eb
430: .getValue(ConfigData.SERVICE_NAMESPACE), eb
431: .getValue(ConfigData.SERVICE_LOCALNAME)), eb
432: .getValue(ConfigData.ENDPOINTNAME));
433: mLog.info(mStringTranslator.getString(
434: FBC_ACTIVATE_OUTBOUND_SUCCESS, eb.getUniqueName()));
435: } catch (JBIException me) {
436: mLog.severe(mStringTranslator.getString(
437: FBC_ACTIVATE_OUTBOUND_FAILED, eb.getUniqueName()));
438: mLog.severe(me.getMessage());
439: setWarning(me.getMessage());
440:
441: return false;
442: }
443:
444: eb.setServiceEndpoint(ref);
445:
446: return true;
447: }
448:
449: /**
450: * Stops all running endpoints. This inclides deactiavting all the
451: * endpoints with the NMS and also stopping all running threads.
452: */
453: public void stopAllEndpoints() {
454: Iterator iter = mRegistry.listAllEndpoints();
455:
456: while (iter.hasNext()) {
457: String ebname = (String) (iter.next());
458:
459: EndpointBean eb = mRegistry.findEndpoint(ebname);
460:
461: try {
462: if (eb.getRole() == ConfigData.CONSUMER) {
463: stopInboundEndpoint(eb);
464: } else if (eb.getRole() == ConfigData.CONSUMER) {
465: stopOutboundEndpoint(eb);
466: }
467: } catch (Exception e) {
468: setWarning(e.getMessage());
469:
470: continue;
471: }
472: }
473: }
474:
475: /**
476: * Stops a deployment.
477: *
478: * @param suid Service unit ID.
479: */
480: public void stopDeployment(String suid) {
481: mLog.info(mStringTranslator
482: .getString(FBC_STOP_DEPLOYMENT, suid));
483: super .clear();
484:
485: List l = mRegistry.getEndpoints(suid);
486: Iterator iter = l.iterator();
487:
488: while (iter.hasNext()) {
489: EndpointBean eb = (EndpointBean) iter.next();
490:
491: if (eb == null) {
492: mLog.severe(mStringTranslator.getString(
493: FBC_STOP_DEPLOYMENT_FAILED, suid));
494: setWarning(mStringTranslator.getString(
495: FBC_STOP_DEPLOYMENT_FAILED, suid));
496:
497: continue;
498: }
499:
500: if (eb.getRole() == ConfigData.PROVIDER) {
501: if (!stopOutboundEndpoint(eb)) {
502: setWarning(mStringTranslator.getString(
503: FBC_STOP_ENDPOINT_FAILED, eb
504: .getUniqueName(), suid));
505:
506: continue;
507: }
508: } else if (eb.getRole() == ConfigData.CONSUMER) {
509: if (!stopInboundEndpoint(eb)) {
510: setWarning(mStringTranslator.getString(
511: FBC_STOP_ENDPOINT_FAILED, eb
512: .getUniqueName(), suid));
513:
514: continue;
515: }
516: }
517: }
518:
519: mLog.info(mStringTranslator.getString(
520: FBC_STOP_DEPLOYMENT_SUCCESS, suid));
521: }
522:
523: /**
524: * Stops a consumer endpoint.
525: *
526: * @param eb endpoint bean.
527: *
528: * @return true if success.
529: */
530: public boolean stopInboundEndpoint(EndpointBean eb) {
531: try {
532: FileThreads ft = (FileThreads) (eb.getFileThread());
533:
534: if (ft.isRunning()) {
535: ft.stopAll();
536: }
537:
538: Thread t = (Thread) eb.getReceiverThread();
539:
540: try {
541: t.interrupt();
542: t.join();
543: } catch (Exception ie) {
544: ;
545: }
546: } catch (Exception e) {
547: mLog.severe(mStringTranslator.getString(
548: FBC_STOP_ENDPOINT_FAILED, eb.getUniqueName()));
549: mLog.severe(e.getMessage());
550: setWarning(e.getMessage());
551:
552: return false;
553: }
554:
555: return true;
556: }
557:
558: /**
559: * Stops a provider endpoint.
560: *
561: * @param eb bean object.
562: *
563: * @return true if success.
564: */
565: public boolean stopOutboundEndpoint(EndpointBean eb) {
566: ServiceEndpoint ref = eb.getServiceEndpoint();
567:
568: try {
569: mContext.deactivateEndpoint(ref);
570: } catch (Exception me) {
571: mLog
572: .severe(mStringTranslator.getString(
573: FBC_DEACTIVATE_OUTBOUND_FAILED, eb
574: .getUniqueName()));
575: mLog.severe(me.getMessage());
576: setWarning(me.getMessage());
577:
578: return false;
579: }
580:
581: return true;
582: }
583: }
|