View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-3-27 下午08:37:50
4    */
5   package gboat2.base.core.validate;
6   
7   import gboat2.base.core.Activator;
8   
9   import java.util.List;
10  import java.util.Map;
11  import java.util.concurrent.CopyOnWriteArrayList;
12  
13  import org.osgi.framework.Bundle;
14  import org.osgi.framework.BundleContext;
15  import org.osgi.framework.Filter;
16  import org.osgi.framework.InvalidSyntaxException;
17  import org.osgi.framework.ServiceReference;
18  import org.osgi.util.tracker.ServiceTracker;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  /**
23   * 注册 PrivilegeCheckService 服务实现
24   * 
25   * @author zhaic
26   * @since 1.0
27   * @date 2012-3-27
28   */
29  
30  public class PrivilegeCheckServiceFactory {
31  
32      private static Logger logger = LoggerFactory.getLogger(PrivilegeCheckServiceFactory.class);
33  
34      private static ServiceTracker<IOperaPrivilegeCheckService, IOperaPrivilegeCheckService> operaPriviTracker;
35  
36      private static ServiceTracker<IDataPrivilegeCheckService, IDataPrivilegeCheckService> dataPriviTracker;
37  
38      /**
39       * 操作权限判断服务
40       */
41      private static List<IOperaPrivilegeCheckService> priCheckSerList = new CopyOnWriteArrayList<IOperaPrivilegeCheckService>();
42  
43      /**
44       * 数据权限判断服务
45       */
46      private static List<IDataPrivilegeCheckService> dataPriCheckSerList = new CopyOnWriteArrayList<IDataPrivilegeCheckService>();
47  
48      public PrivilegeCheckServiceFactory() {
49          BundleContext context = Activator.LOCAL_BUNDLE.getBundleContext();
50          try {
51              Filter operaFilter = context.createFilter("(objectClass=gboat2.base.core.validate.IOperaPrivilegeCheckService)");
52              operaPriviTracker = new ServiceTracker<IOperaPrivilegeCheckService, IOperaPrivilegeCheckService>(context, operaFilter, null) {
53                  @Override
54                  public IOperaPrivilegeCheckService addingService(ServiceReference<IOperaPrivilegeCheckService> refer) {
55                      IOperaPrivilegeCheckService operaPrivilege = context.getService(refer);
56                      registValidateService(operaPrivilege);
57                      return super.addingService(refer);
58                  }
59  
60                  @Override
61                  public void remove(ServiceReference<IOperaPrivilegeCheckService> refer) {
62                      IOperaPrivilegeCheckService operaPrivilege = context.getService(refer);
63                      unRegistValidateService(operaPrivilege);
64                  }
65              };
66              operaPriviTracker.open();
67          } catch (InvalidSyntaxException e) {
68              logger.error("", e);
69          }
70  
71          try {
72              Filter dataFilter = context.createFilter("(objectClass=gboat2.base.core.validate.PrivilegeCheckService)");
73              dataPriviTracker = new ServiceTracker<IDataPrivilegeCheckService, IDataPrivilegeCheckService>(
74                      Activator.LOCAL_BUNDLE.getBundleContext(), dataFilter, null) {
75  
76                  @Override
77                  public IDataPrivilegeCheckService addingService(ServiceReference<IDataPrivilegeCheckService> refer) {
78                      IDataPrivilegeCheckService dataPrivilege =  context.getService(refer);
79                      registDataValidateService(dataPrivilege);
80                      return dataPrivilege;
81                  }
82  
83                  @Override
84                  public void remove(ServiceReference<IDataPrivilegeCheckService> refer) {
85                      IDataPrivilegeCheckService dataPrivilege =  context.getService(refer);
86                      unRegistDataValidateService(dataPrivilege);
87                  }
88              };
89              dataPriviTracker.open();
90          } catch (InvalidSyntaxException e) {
91              logger.error("", e);
92          }
93      }
94  
95      public static void close() {
96          if (operaPriviTracker != null) {
97              operaPriviTracker.close();
98          }
99          if (dataPriviTracker != null) {
100             dataPriviTracker.close();
101         }
102     }
103 
104     public static List<IOperaPrivilegeCheckService> getPrivilegeSerivce() {
105         return priCheckSerList;
106     }
107 
108     public static List<IDataPrivilegeCheckService> getDataPrivelegeService() {
109         return dataPriCheckSerList;
110     }
111 
112     private void registValidateService(IOperaPrivilegeCheckService priCheckSer) {
113         logger.info("regist opera validate service : {}", priCheckSer.getClass().getName());
114         priCheckSerList.add(priCheckSer);
115     }
116 
117     private void unRegistValidateService(IOperaPrivilegeCheckService validateServ) {
118         logger.info("unregist opera validate service : {}", validateServ.getClass().getName());
119         priCheckSerList.remove(validateServ);
120     }
121 
122     private void registDataValidateService(IDataPrivilegeCheckService validateServ) {
123         logger.info("regist data validate service : {}", validateServ.getClass().getName());
124         dataPriCheckSerList.add(validateServ);
125     }
126 
127     private void unRegistDataValidateService(IDataPrivilegeCheckService validateServ) {
128         logger.info("unregist data validate service : {}", validateServ.getClass().getName());
129         dataPriCheckSerList.remove(validateServ);
130     }
131 
132     /**
133      * 循环判断是否具有该操作
134      * 
135      * @param params 操作集合
136      * @param bd 当前bundle实例
137      * @return 权限验证 有权限 返回 true 否则 false
138      */
139     public static boolean operationCheckServiceByMethodCode(Map<?, ?> params) {
140         for (IOperaPrivilegeCheckService tempCheckSer : priCheckSerList) {
141             if (!tempCheckSer.privilegeCheckServiceByMethodCode(params, null))
142                 return Boolean.FALSE;
143         }
144         return Boolean.TRUE;
145     }
146 
147     /**
148      * 循环判断是否具有该操作
149      * 
150      * @param params 操作集合
151      * @param bd 当前bundle实例
152      * @return 权限验证 有权限 返回 true 否则 false
153      */
154     public static boolean operationCheckServiceByMethodName(Map<?, ?> params, Bundle bd) {
155         for (IOperaPrivilegeCheckService tempCheckSer : priCheckSerList) {
156             if (!tempCheckSer.privilegeCheckServiceByMethodName(params, bd))
157                 return Boolean.FALSE;
158         }
159         return Boolean.TRUE;
160     }
161 
162     public static boolean operationDataCheckService(Map<?, ?> params, Bundle bd) {
163         for (IDataPrivilegeCheckService tempCheckSer : dataPriCheckSerList) {
164             if (!tempCheckSer.privilegeCheckServiceByMethodName(params, bd))
165                 return Boolean.FALSE;
166         }
167         return Boolean.TRUE;
168     }
169 }