KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > clock > Clock


1 package org.objectweb.petals.engine.clock;
2
3 import java.io.File JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.logging.Level JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import javax.jbi.JBIException;
10 import javax.jbi.component.Component;
11 import javax.jbi.component.ComponentContext;
12 import javax.jbi.component.ComponentLifeCycle;
13 import javax.jbi.component.ServiceUnitManager;
14 import javax.jbi.messaging.DeliveryChannel;
15 import javax.jbi.messaging.MessageExchange;
16 import javax.jbi.messaging.MessagingException;
17 import javax.jbi.servicedesc.ServiceEndpoint;
18 import javax.management.ObjectName JavaDoc;
19 import javax.xml.namespace.QName JavaDoc;
20
21 import org.objectweb.petals.shared.xmlmanipulation.XMLHelper;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.DocumentFragment JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25
26 public class Clock implements Component, ComponentLifeCycle {
27     
28     private ComponentContext context;
29     
30     private DeliveryChannel channel;
31     
32     @SuppressWarnings JavaDoc("unused")
33     private ServiceEndpoint endpointReference;
34     
35     private ClockListener listener;
36     
37     private static final String JavaDoc SERVICE = "ClockService";
38     private static final String JavaDoc ENDPOINT = "ClockEndpoint";
39     
40     private Logger JavaDoc logger;
41     
42     private Map JavaDoc<String JavaDoc,TimeZonesMap> timeZoneMaps;
43     
44     public void init(ComponentContext context) throws JBIException
45     {
46         this.context = context;
47         
48         logger = context.getLogger("",null);
49
50         logger.log(Level.INFO,"");
51         
52         timeZoneMaps = new HashMap JavaDoc<String JavaDoc,TimeZonesMap>();
53     }
54
55     
56     public void shutDown() throws JBIException
57     {
58         this.listener.stopProcessing();
59     }
60
61     
62     public void start() throws JBIException
63     {
64         logger.log(Level.INFO,"");
65         
66         try
67         {
68             this.channel = this.context.getDeliveryChannel();
69             this.listener = new ClockListener(this.channel,logger);
70         
71             Thread JavaDoc listenerThread = new Thread JavaDoc(this.listener);
72             listenerThread.start();
73             
74             endpointReference = this.context.activateEndpoint(new QName JavaDoc(SERVICE), ENDPOINT);
75         }
76         catch (MessagingException e)
77         {
78             throw new JBIException(e);
79         }
80
81     }
82
83     
84     public void stop() throws JBIException {
85         logger.log(Level.INFO,"");
86         
87         this.listener.stopProcessing();
88     }
89     
90     
91     public ComponentLifeCycle getLifeCycle()
92     {
93         return this;
94     }
95
96     public Document JavaDoc getServiceDescription(ServiceEndpoint arg0)
97     {
98         if( new QName JavaDoc(SERVICE).equals(arg0.getServiceName()))
99         {
100             File JavaDoc wsdlFile = new File JavaDoc(context.getInstallRoot()+"/META-INF/ClockService.wsdl");
101     
102             return XMLHelper.createDocumentFromWSDL(wsdlFile);
103         }
104         return null;
105     }
106
107     public ServiceUnitManager getServiceUnitManager()
108     {
109         return new ClockServiceUnitManager(this);
110     }
111
112     public boolean isExchangeWithConsumerOkay(ServiceEndpoint arg0,
113             MessageExchange arg1) {
114         logger.log(Level.INFO,"accept the exchange");
115         return true;
116     }
117
118     public boolean isExchangeWithProviderOkay(ServiceEndpoint arg0,
119             MessageExchange arg1) {
120         return false;
121     }
122
123     public ServiceEndpoint resolveEndpointReference(DocumentFragment JavaDoc arg0) {
124         ServiceEndpoint result = null;
125         
126         if (arg0 != null)
127         {
128             Node JavaDoc service = XMLHelper.findChild(arg0,"service",true);
129
130             if (service != null)
131             {
132                 String JavaDoc serviceRequested = XMLHelper.getAttributeValue(service,"name");
133                 
134                 if (SERVICE.equals(serviceRequested))
135                 {
136                     result = endpointReference;
137                 }
138             }
139         }
140         return result;
141     }
142
143     public ObjectName JavaDoc getExtensionMBeanName() {
144         // TODO Auto-generated method stub
145
return null;
146     }
147     
148     protected TimeZonesMap getTimeZonesForArtifact(String JavaDoc artifactName){
149         return timeZoneMaps.get(artifactName);
150     }
151
152     protected void addTimeZonesForArtifact(String JavaDoc artifactName, TimeZonesMap tzm){
153         timeZoneMaps.put(artifactName,tzm);
154     }
155
156     protected void removeTimeZonesForArtifact(String JavaDoc artifactName){
157         timeZoneMaps.remove(artifactName);
158     }
159
160     protected int getOffSet (String JavaDoc city){
161         Integer JavaDoc tz=null;
162         for (TimeZonesMap tzm : timeZoneMaps.values()){
163             tz = tzm.getTimeZone(city);
164         }
165         if (tz!=null){
166             return tz.intValue();
167         }
168         else {
169             return 0;
170         }
171     }
172 }
173
Popular Tags