001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.bpmscript.jbi.tasklist;
018:
019: import javax.jbi.component.ComponentContext;
020: import javax.jbi.management.DeploymentException;
021: import javax.jbi.messaging.DeliveryChannel;
022: import javax.jbi.messaging.ExchangeStatus;
023: import javax.jbi.messaging.MessageExchange;
024: import javax.jbi.messaging.MessageExchangeFactory;
025: import javax.jbi.messaging.MessagingException;
026: import javax.jbi.messaging.MessageExchange.Role;
027: import javax.jbi.servicedesc.ServiceEndpoint;
028:
029: import org.apache.servicemix.common.BaseLifeCycle;
030: import org.apache.servicemix.common.Endpoint;
031: import org.apache.servicemix.common.ExchangeProcessor;
032: import org.apache.servicemix.jbi.messaging.DefaultMarshaler;
033: import org.apache.servicemix.jbi.messaging.PojoMarshaler;
034:
035: /**
036: * @org.apache.xbean.XBean element="endpoint"
037: */
038: public class TaskListEndpoint extends Endpoint implements
039: ExchangeProcessor {
040:
041: private ServiceEndpoint activated;
042: private DeliveryChannel channel;
043: private MessageExchangeFactory exchangeFactory;
044: private ITaskListStore taskListStore;
045: private PojoMarshaler marshaler = new DefaultMarshaler();
046:
047: /* (non-Javadoc)
048: * @see org.apache.servicemix.common.Endpoint#getRole()
049: */
050: public Role getRole() {
051: return Role.PROVIDER;
052: }
053:
054: public void activate() throws Exception {
055: logger = this .serviceUnit.getComponent().getLogger();
056: ComponentContext ctx = getServiceUnit().getComponent()
057: .getComponentContext();
058: channel = ctx.getDeliveryChannel();
059: exchangeFactory = channel.createExchangeFactory();
060: activated = ctx.activateEndpoint(service, endpoint);
061: start();
062: }
063:
064: public void deactivate() throws Exception {
065: stop();
066: ServiceEndpoint ep = activated;
067: activated = null;
068: ComponentContext ctx = getServiceUnit().getComponent()
069: .getComponentContext();
070: ctx.deactivateEndpoint(ep);
071: }
072:
073: public ExchangeProcessor getProcessor() {
074: return this ;
075: }
076:
077: public void validate() throws DeploymentException {
078: }
079:
080: protected void send(MessageExchange me) throws MessagingException {
081: if (me.getRole() == MessageExchange.Role.CONSUMER
082: && me.getStatus() == ExchangeStatus.ACTIVE) {
083: BaseLifeCycle lf = (BaseLifeCycle) getServiceUnit()
084: .getComponent().getLifeCycle();
085: lf.sendConsumerExchange(me, (Endpoint) this );
086: } else {
087: channel.send(me);
088: }
089: }
090:
091: protected void done(MessageExchange me) throws MessagingException {
092: me.setStatus(ExchangeStatus.DONE);
093: send(me);
094: }
095:
096: protected void fail(MessageExchange me, Exception error)
097: throws MessagingException {
098: me.setError(error);
099: send(me);
100: }
101:
102: public void start() throws Exception {
103: }
104:
105: public void stop() {
106: }
107:
108: public void process(MessageExchange exchange) throws Exception {
109: if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
110: ITaskRequest request = (ITaskRequest) marshaler.unmarshal(
111: exchange, exchange.getMessage("in"));
112: try {
113: taskListStore.createTask(exchange, request);
114: } catch (TaskListException e) {
115: fail(exchange, e);
116: }
117: }
118: }
119:
120: public void setMarshaler(PojoMarshaler marshaler) {
121: this .marshaler = marshaler;
122: }
123:
124: public void setTaskListStore(ITaskListStore taskListStore) {
125: this.taskListStore = taskListStore;
126: }
127:
128: }
|