All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jianggujin.modulelink.util.JModuleUtils Maven / Gradle / Ivy

/**
 * Copyright 2018 jianggujin (www.jianggujin.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jianggujin.modulelink.util;

import com.jianggujin.modulelink.JAction;
import com.jianggujin.modulelink.JModule;
import com.jianggujin.modulelink.JModuleLinkException;
import com.jianggujin.modulelink.util.JLogFactory.JLog;

/**
 * 模块工具类
 *
 */
public class JModuleUtils {

   private static final JLog logger = JLogFactory.getLog(JModuleUtils.class);

   /**
    * 销毁模块,不抛出异常
    *
    * @param module
    */
   public static void destroyQuietly(JModule module) {
      if (module != null) {
         try {
            if (logger.isDebugEnabled()) {
               logger.debug("Destroy module: " + module.getModuleConfig());
            }
            module.destroy();
         } catch (Exception e) {
            logger.error("Failed to destroy module " + module.getModuleConfig(), e);
         }
      }
   }

   /**
    * 调用Action处理请求,注意的是执行时应该用Action的ClassLoader
    * 
    * @param action
    * @param in
    * @return
    */
   public static Object doActionWithinModuleClassLoader(JAction action, Object in) {
      JAssert.checkNotNull(action, "action must not be null");
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      try {
         ClassLoader moduleClassLoader = action.getClass().getClassLoader();
         Thread.currentThread().setContextClassLoader(moduleClassLoader);
         return action.execute(in);
      } catch (Exception e) {
         logger.error("doActionWithinModuleClassLoader error, action=" + action, e);
         throw new JModuleLinkException("doActionWithinModuleClassLoader has error, action=" + action, e);
      } finally {
         Thread.currentThread().setContextClassLoader(classLoader);
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy