/** 通过一个类型获取所有绑定到该类型的上的对象实例。 */
 public <T> Provider<T> findBindingProvider(final String withName, final Class<T> bindType) {
   Hasor.assertIsNotNull(withName, "withName is null.");
   Hasor.assertIsNotNull(bindType, "bindType is null.");
   //
   BindInfo<T> typeRegister = this.findBindingRegister(withName, bindType);
   if (typeRegister != null) {
     return this.getProvider(typeRegister);
   }
   return null;
 }
 /** 通过一个类型获取所有绑定到该类型的上的对象实例。 */
 public <T> BindInfo<T> findBindingRegister(final String withName, final Class<T> bindType) {
   Hasor.assertIsNotNull(withName, "withName is null.");
   Hasor.assertIsNotNull(bindType, "bindType is null.");
   //
   BeanContainer container = getContainer();
   List<BindInfo<T>> typeRegisterList = container.getBindInfoByType(bindType);
   if (typeRegisterList != null && typeRegisterList.isEmpty() == false) {
     for (BindInfo<T> adapter : typeRegisterList) {
       if (StringUtils.equals(adapter.getBindName(), withName)) {
         return adapter;
       }
     }
   }
   return null;
 }
Exemple #3
0
 private Map<String, List<String>> getQueryParamMap() {
   if (queryParamLocal != null) return queryParamLocal;
   //
   HttpServletRequest httpRequest = getRequest();
   String queryString = httpRequest.getQueryString();
   if (StringUtils.isBlank(queryString)) return null;
   //
   queryParamLocal = new HashMap<String, List<String>>();
   String[] params = queryString.split("&");
   for (String pData : params) {
     String oriData = null;
     String encoding = httpRequest.getCharacterEncoding();
     try {
       oriData = URLDecoder.decode(pData, encoding);
     } catch (Exception e) {
       Hasor.logWarn("use ‘%s’ decode ‘%s’ error.", encoding, pData);
       continue;
     }
     String[] kv = oriData.split("=");
     if (kv.length < 2) continue;
     String k = kv[0].trim().toUpperCase();
     String v = kv[1];
     //
     List<String> pArray = queryParamLocal.get(k);
     pArray = pArray == null ? new ArrayList<String>() : pArray;
     if (pArray.contains(v) == false) pArray.add(v);
     queryParamLocal.put(k, pArray);
   }
   return queryParamLocal;
 }
 /** 创建Bean。 */
 public <T> Provider<T> getProvider(final Class<T> targetClass) {
   Hasor.assertIsNotNull(targetClass, "targetClass is null.");
   Provider<T> targetProvider = null;
   //
   final BeanContainer container = getContainer();
   List<BindInfo<T>> typeRegisterList = container.getBindInfoByType(targetClass);
   if (typeRegisterList != null && typeRegisterList.isEmpty() == false) {
     for (int i = typeRegisterList.size() - 1; i >= 0; i--) {
       BindInfo<T> adapter = typeRegisterList.get(i);
       if (adapter.getBindName() == null) {
         Provider<T> provider = this.getProvider(adapter);
         if (provider != null) {
           targetProvider = provider;
           break;
         }
       }
     }
   }
   if (targetProvider == null) {
     final AppContext appContext = this;
     targetProvider =
         new Provider<T>() {
           public T get() {
             return getBeanBuilder().getDefaultInstance(targetClass, appContext);
           }
         };
   }
   return targetProvider;
 }
Exemple #5
0
 /** 执行初始化 */
 protected void init(AppContext appContext) {
   if (init == true) {
     return;
   }
   Hasor.assertIsNotNull(appContext, "appContext is null.");
   BindInfo<ModelController> controllerInfo = appContext.getBindInfo(this.bindID);
   this.targetProvider = appContext.getProvider(controllerInfo);
   this.init = true;
 }
 /** 通过名获取Bean的类型。 */
 public Class<?> getBeanType(String bindID) {
   Hasor.assertIsNotNull(bindID, "bindID is null.");
   BeanContainer container = getContainer();
   BindInfo<?> bindInfo = container.getBindInfoByID(bindID);
   if (bindInfo != null) {
     return bindInfo.getBindType();
   }
   return null;
 }
 //    public void removeAllEventListener(String eventType) {
 //        this.listenerRWLock.writeLock().lock();//加锁(写)
 //        this.listenerMap.remove(eventType);
 //        this.listenerRWLock.writeLock().unlock();//解锁(写)
 //    }
 public void removeListener(String eventType, EventListener eventListener) {
   this.listenerRWLock.writeLock().lock(); // 加锁(写)
   //
   Hasor.assertIsNotNull(eventType, "remove eventType is null.");
   Hasor.assertIsNotNull(eventListener, "remove EventListener object is null.");
   EventListener[] eventListenerArray = this.listenerMap.get(eventType);
   if (!ArrayUtils.isEmpty(eventListenerArray)) {
     int index = ArrayUtils.indexOf(eventListenerArray, eventListener);
     eventListenerArray =
         (EventListener[])
             ((index == ArrayUtils.INDEX_NOT_FOUND)
                 ? eventListenerArray
                 : ArrayUtils.remove(eventListenerArray, index));
     this.listenerMap.put(eventType, eventListenerArray);
   }
   //
   this.listenerRWLock.writeLock().unlock(); // 解锁(写)
 }
 /** 创建Bean。 */
 public <T> Provider<T> getProvider(String bindID) {
   Hasor.assertIsNotNull(bindID, "bindID is null.");
   BeanContainer container = getContainer();
   BindInfo<T> bindInfo = container.getBindInfoByID(bindID);
   if (bindInfo != null) {
     return this.getProvider(bindInfo);
   }
   return null;
 }
 /** 如果存在目标类型的Bean则返回Bean的名称。 */
 public String[] getNames(final Class<?> targetClass) {
   Hasor.assertIsNotNull(targetClass, "targetClass is null.");
   BeanContainer container = getContainer();
   Collection<String> nameList = container.getBindInfoNamesByType(targetClass);
   if (nameList == null || nameList.isEmpty() == true) {
     return ArrayUtils.EMPTY_STRING_ARRAY;
   }
   return nameList.toArray(new String[nameList.size()]);
 }
 /** 创建Bean。 */
 public <T> T getInstance(Class<T> targetClass) {
   Hasor.assertIsNotNull(targetClass, "targetClass is null.");
   Provider<T> provider = this.getProvider(targetClass);
   if (provider != null) {
     logger.debug("getInstance form getProvider, targetClass is {}.", targetClass);
     return provider.get();
   } else {
     logger.debug("getInstance form getDefaultInstance, targetClass is {}.", targetClass);
     return getBeanBuilder().getDefaultInstance(targetClass, this);
   }
 }
 public StandardEventManager(Environment env) {
   env = Hasor.assertIsNotNull(env, "Environment type parameter is empty!");
   this.settings = env.getSettings();
   this.executorService = Executors.newScheduledThreadPool(1);
   env.addSettingsListener(
       new SettingsListener() {
         public void reload(Settings newConfig) {
           update();
         }
       });
   this.update();
 }
 /** 通过一个类型获取所有绑定到该类型的上的对象实例。 */
 public <T> List<Provider<T>> findBindingProvider(final Class<T> bindType) {
   Hasor.assertIsNotNull(bindType, "bindType is null.");
   //
   BeanContainer container = getContainer();
   List<BindInfo<T>> typeRegisterList = container.getBindInfoByType(bindType);
   if (typeRegisterList == null || typeRegisterList.isEmpty()) {
     return new ArrayList<Provider<T>>(0);
   }
   ArrayList<Provider<T>> returnData = new ArrayList<Provider<T>>();
   for (BindInfo<T> adapter : typeRegisterList) {
     Provider<T> provider = this.getProvider(adapter);
     returnData.add(provider);
   }
   return returnData;
 }
 /** 通过一个类型获取所有绑定到该类型的上的对象实例。 */
 public <T> List<BindInfo<T>> findBindingRegister(final Class<T> bindType) {
   Hasor.assertIsNotNull(bindType, "bindType is null.");
   //
   BeanContainer container = getContainer();
   List<BindInfo<T>> typeRegisterList = container.getBindInfoByType(bindType);
   if (typeRegisterList == null || typeRegisterList.isEmpty()) {
     logger.debug("findBindingRegister , never define this bindType = {}", bindType);
     return new ArrayList<BindInfo<T>>(0);
   }
   ArrayList<BindInfo<T>> returnData = new ArrayList<BindInfo<T>>();
   for (BindInfo<T> adapter : typeRegisterList) {
     returnData.add(adapter);
   }
   return returnData;
 }
 public void addListener(String eventType, EventListener eventListener) {
   this.listenerRWLock.writeLock().lock(); // 加锁(写)
   //
   Hasor.assertIsNotNull(eventListener, "add EventListener object is null.");
   EventListener[] eventListenerArray = this.listenerMap.get(eventType);
   if (eventListenerArray == null) {
     eventListenerArray = new EventListener[] {eventListener};
     this.listenerMap.put(eventType, eventListenerArray);
   } else {
     if (ArrayUtils.contains(eventListenerArray, eventListener) == false) {
       eventListenerArray = (EventListener[]) ArrayUtils.add(eventListenerArray, eventListener);
       this.listenerMap.put(eventType, eventListenerArray);
     }
   }
   //
   this.listenerRWLock.writeLock().unlock(); // 解锁(写)
 }
Exemple #15
0
 public <T> T execute(final SqlSessionCallback<T> sessionCallback) throws SQLException {
   Hasor.assertIsNotNull(sessionCallback, "Callback object must not be null");
   //
   ConnectionCallback<T> callBack =
       new ConnectionCallback<T>() {
         public T doInConnection(Connection con) throws SQLException {
           SqlSession sqlSession = sessionFactory.openSession(con);
           try {
             T result = sessionCallback.doSqlSession(sqlSession);
             return result;
           } finally {
             if (sqlSession != null) {
               sqlSession.close();
             }
           }
         }
       };
   return this.execute(callBack);
 }
 private void processOnceListener(
     boolean ignore, String eventType, AsyncCallBackHook callBack, Object... objects) {
   this.onceListenerLock.lock(); // 加锁
   LinkedList<EventListener> eventList = this.onceListenerMap.get(eventType);
   if (eventList != null) {
     EventListener listener = null;
     while ((listener = eventList.pollLast()) != null) {
       try {
         listener.onEvent(eventType, objects);
       } catch (Throwable e) {
         if (ignore)
           Hasor.logWarn(
               "During the execution of OnceListener ‘%s’ throw an error.%s",
               listener.getClass(), e);
         else callBack.handleException(eventType, objects, e);
       }
     }
   }
   this.onceListenerLock.unlock(); // 解锁
 }
 private void _doSyncEvent(boolean ignore, String eventType, Object... objects) throws Throwable {
   if (StringUtils.isBlank(eventType) == true) return;
   this.listenerRWLock.readLock().lock(); // 加锁(读)
   EventListener[] eventListenerArray = this.listenerMap.get(eventType);
   this.listenerRWLock.readLock().unlock(); // 解锁(读)
   //
   if (eventListenerArray != null) {
     for (EventListener event : eventListenerArray)
       try {
         event.onEvent(eventType, objects);
       } catch (Throwable e) {
         if (ignore)
           Hasor.logWarn(
               "During the execution of SyncEvent ‘%s’ throw an error.%s", event.getClass(), e);
         else throw e;
       }
   }
   // 处理OnceListener
   this.processOnceListener(ignore, eventType, EmptyAsyncCallBack, objects);
 }
 /** @return 判断是否存在某个ID的绑定。 */
 public boolean containsBindID(String bindID) {
   Hasor.assertIsNotNull(bindID, "bindID is null.");
   BeanContainer container = getContainer();
   BindInfo<?> bindInfo = container.getBindInfoByID(bindID);
   return bindInfo != null;
 }
Exemple #19
0
 /** 测试路径是否匹配 */
 public boolean matchingMapping(String requestPath) {
   Hasor.assertIsNotNull(requestPath, "requestPath is null.");
   return requestPath.matches(this.mappingInfo.getMappingToMatches());
 }