package com.vci.server; import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.omg.PortableServer.POA; import org.omg.PortableServer.Servant; import com.vci.common.log.ServerWithLog4j; import com.vci.common.utility.ClassLoaderUtil; import com.vci.server.base.corbautil.NameServiceUtility; import com.vci.server.base.persistence.dao.HibernateSessionFactory; public class ServiceManager { private static String MAP_FILE_RESOURCE="/properties/hibernate.map.xml"; protected static NameServiceUtility nsu = null; protected void loadJar(String jarFile) { File file = new File(jarFile); ClassLoaderUtil.loadJarFile(file); } protected void addHbmFile(Class c) { try { InputStream is = c.getResourceAsStream(MAP_FILE_RESOURCE); if (is != null) { SAXReader reader = new SAXReader(); Document doc; doc = reader.read(is); Element root = doc.getRootElement(); List lst = root.elements("mapping"); List lstHbm = new ArrayList(); for (Element el : lst) { Attribute at = el.attribute("resource"); if (at != null) { String hbmFile = at.getValue(); lstHbm.add(hbmFile); } } if (lstHbm.size() > 0) { HibernateSessionFactory.setHbmFiles(lstHbm.toArray(new String[0])); } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 绑定所有的corba服务 */ protected void bindService(String name, Servant servant) { long time = System.currentTimeMillis(); String token = new java.math.BigInteger(Long.toString(time) , 10).toString(32).toUpperCase(); //String token = Long.toHexString(time).toUpperCase(); String serviceName = token + "." + name; try { nsu = new NameServiceUtility(); nsu.InitORBServerEnv(); nsu.InitORB(); nsu.InitNameContext(); POA poaRoot = nsu.getPoa(); ServerWithLog4j.logger.info("绑定业务服务: " + serviceName); poaRoot.activate_object(servant); nsu.bindToNs(serviceName, poaRoot.servant_to_reference(servant)); ServerWithLog4j.logger.info("bind " + serviceName + " OK! Class:" + servant.getClass().getName()); nsu.getOrb().run(); } catch(Exception e) { e.printStackTrace(); ServerWithLog4j.logger.error(e.getMessage(), e); } finally { ServerWithLog4j.logger.info("unbind " + serviceName + " begin..."); unBindService(serviceName); nsu.close(); nsu = null; } } /** * 解除绑定所有的Corba服务 */ protected void unBindService(String name) { try { if (nsu == null) return; ServerWithLog4j.logger.info("unbind " + name + " begin..."); nsu.unBindToNs(name); ServerWithLog4j.logger.info("unbind " + name + " OK!"); } catch(Exception e) { e.printStackTrace(); } } }