田源
2024-07-23 89bdfef24b9ee62719e8a9def5151c3ebb1224f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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<Element> lst = root.elements("mapping");
                
                List<String> lstHbm = new ArrayList<String>();
                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();
        }
    }
    
}