View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-5-8 下午02:03:02
4    */
5   package gboat2.serviceflow.service.impl;
6   
7   import gboat2.serviceflow.Activator;
8   import gboat2.serviceflow.service.IEntryActionHandler;
9   import gboat2.serviceflow.service.IExitActionHandler;
10  import gboat2.serviceflow.service.IHandlerRegister;
11  
12  import java.util.LinkedList;
13  import java.util.List;
14  
15  import org.apache.log4j.Logger;
16  import org.osgi.framework.BundleContext;
17  import org.osgi.framework.Filter;
18  import org.osgi.framework.InvalidSyntaxException;
19  import org.osgi.framework.ServiceReference;
20  import org.osgi.util.tracker.ServiceTracker;
21  
22  /**
23   * 
24   * @author lysming
25   * @since jdk1.6
26   * @date 2012-5-8
27   *  
28   */
29  
30  public class HandlerRegisterImpl implements IHandlerRegister {
31  	ServiceTracker exitActionTracker;
32  	ServiceTracker entryActionTracker;
33  	
34  	private static Logger logger = Logger.getLogger(HandlerRegisterImpl.class);
35  	
36  	private List<IEntryActionHandler> entryActionHandlers = new LinkedList<IEntryActionHandler>();
37  	
38  	private List<IExitActionHandler> exitActionHandlers = new LinkedList<IExitActionHandler>();
39  	
40  	public HandlerRegisterImpl(){
41  		BundleContext context = Activator.LOCAL_BUNDLE.getBundleContext();
42  		try {
43  			Filter exitFilter = context.createFilter("(objectClass=gboat2.serviceflow.service.IExitActionHandler)");
44  			exitActionTracker = new ServiceTracker(Activator.LOCAL_BUNDLE.getBundleContext(),exitFilter,null){
45  				@Override
46  				public Object addingService(ServiceReference refer){
47  					IExitActionHandler handler = (IExitActionHandler)context.getService(refer);
48  					registExitActionHandler(handler);
49  					return handler;
50  				}
51  				
52  				@Override
53  				public void remove(ServiceReference refer){
54  					IExitActionHandler handler = (IExitActionHandler)context.getService(refer);
55  					unRegistExitActionHandler(handler);
56  					super.remove(refer);
57  				}
58  			};
59  			exitActionTracker.open();
60  		} catch (InvalidSyntaxException e) {
61  			logger.error(e);
62  		}
63  		
64  		try {
65  			Filter entryFilter = context.createFilter("(objectClass=gboat2.serviceflow.service.IEntryActionHandler)");
66  			entryActionTracker = new ServiceTracker(Activator.LOCAL_BUNDLE.getBundleContext(),entryFilter,null){
67  				@Override
68  				public Object addingService(ServiceReference refer){
69  					IEntryActionHandler handler = (IEntryActionHandler)context.getService(refer);
70  					registEntryActionHandler(handler);
71  					return handler;
72  				}
73  				
74  				@Override
75  				public void remove(ServiceReference refer){
76  					IEntryActionHandler handler = (IEntryActionHandler)context.getService(refer);
77  					unRegistEntryActionHandler(handler);
78  					super.remove(refer);
79  				}
80  			};
81  			entryActionTracker.open();
82  		} catch (InvalidSyntaxException e) {
83  			logger.error(e);
84  		}
85  	}
86  	
87  	private synchronized void registEntryActionHandler(IEntryActionHandler entryActionHandler) {
88  		entryActionHandlers.add(entryActionHandler);
89  	}
90  	
91  	private synchronized void registExitActionHandler(IExitActionHandler exitActionHandler) {
92  		exitActionHandlers.add(exitActionHandler);
93  	}
94  	
95  	public List<IEntryActionHandler> getEntryActionHandlers() {
96  		return entryActionHandlers;
97  	}
98  	
99  	public List<IExitActionHandler> getExitActionHandlers() {
100 		return exitActionHandlers;
101 	}
102 	
103 	public synchronized void unRegistEntryActionHandler(IEntryActionHandler entryActionHandler) {
104 		entryActionHandlers.remove(entryActionHandler);
105 	}
106 	
107 	public synchronized void unRegistExitActionHandler(IExitActionHandler exitActionHandler) {
108 		entryActionHandlers.remove(exitActionHandler);
109 	}
110 }