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