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: * @(#)FileReceiver.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.framework.WorkManager;
033: import com.sun.jbi.binding.file.util.StringTranslator;
034:
035: import java.util.logging.Logger;
036:
037: import javax.jbi.messaging.DeliveryChannel;
038: import javax.jbi.messaging.MessageExchange;
039:
040: /**
041: * The File Reciever class receives the Normalized Message from the NMR and
042: * writes the contents to the directory specified in file.xml
043: *
044: * @author Sun Microsystems, Inc.
045: */
046: class FileReceiver implements Runnable, FileBindingResources {
047: /**
048: * Time out for receive
049: */
050: private static final long TIME_OUT = 500;
051:
052: /**
053: * Wait time for thread.
054: */
055: private static final long WAIT_TIME = 100;
056:
057: /**
058: * Receiver.
059: */
060: private static final String RECEIVER = "RECEIVER";
061:
062: /**
063: * Binding channel.
064: */
065: private DeliveryChannel mChannel;
066:
067: /**
068: * Logger Object
069: */
070: private Logger mLog;
071:
072: /**
073: * Normalized Message
074: */
075: private MessageExchange mExchange;
076:
077: /**
078: * Monitor Object to stop the thread.
079: */
080: private Object mMonitor;
081:
082: /**
083: * Helper for i18n.
084: */
085: private StringTranslator mTranslator;
086:
087: /**
088: * Thread framework object.
089: */
090: private WorkManager mWorkManager;
091:
092: /**
093: * Creates the FileReceiver Thread.
094: *
095: * @param bc Thread group for this receiver
096: */
097: public FileReceiver(DeliveryChannel bc) {
098: mLog = FileBindingContext.getInstance().getLogger();
099: mTranslator = new StringTranslator();
100: mChannel = bc;
101: mWorkManager = WorkManager.getWorkManager(RECEIVER);
102: mMonitor = new Object();
103: }
104:
105: /**
106: * Blocking call on the service channel to receive the message.
107: */
108: public void run() {
109: mLog.info(mTranslator.getString(FBC_RECEIVER_START));
110: mWorkManager.start();
111:
112: while (mMonitor != null) {
113: try {
114: mExchange = mChannel.accept(TIME_OUT);
115:
116: if (mExchange != null) {
117: MessageProcessor proc = new MessageProcessor(
118: mChannel, mExchange);
119: mLog.info(mTranslator
120: .getString(FBC_MESSAGE_RECEIVED));
121:
122: if (!mWorkManager.processCommand(proc)) {
123: mLog.info(mTranslator
124: .getString(FBC_NO_FREE_THREAD));
125: }
126: }
127: } catch (Exception e) {
128: mLog.info(mTranslator.getString(FBC_RECEIVER_ERROR));
129: mLog.info(e.getMessage());
130: mWorkManager.cease();
131:
132: return;
133: }
134: }
135: }
136:
137: /**
138: * Stops the receiving thread.
139: */
140: public void stopReceiving() {
141: mLog.fine(mTranslator.getString(FBC_RECEIVER_STOP));
142: mMonitor = null;
143: }
144: }
|