package com.jqmobile.core.server; import java.io.IOException; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.jqmobile.core.server.rmi.RemoteServiceFactory; import com.jqmobile.core.server.rmi.RmiService; import com.jqmobile.core.server.rmi2.RmiService2; import com.jqmobile.core.server.servlet.IWSFilter; import com.jqmobile.core.server.servlet.load.LoadListen; import com.jqmobile.core.server.servlet2.CoreWSFilter; import com.jqmobile.core.service.InitService; import com.jqmobile.core.service.Service; import com.jqmobile.core.utils.plain.Log; import com.jqmobile.core.utils.xml.IReadXML; import com.jqmobile.core.utils.xml.IXMLNode; import com.jqmobile.core.utils.xml.IXMLParse; import com.jqmobile.core.utils.xml.impl.ReadXML; public class Application { private static Application app; private static final Object lock = new Object(); private RemoteServiceFactory lbsRemote; private final List<RemoteAddress> remoteAddress = new ArrayList<Application.RemoteAddress>(); private final List<RemoteUser> remoteUser = new ArrayList<Application.RemoteUser>(); //=============================================================== @Deprecated private final static List<IWSFilter> filters = new ArrayList<IWSFilter>(); @Deprecated private static LoadListen uploadAdapter; @Deprecated public static void addWSFilter(IWSFilter filter){ filters.add(filter); } @Deprecated public static List<IWSFilter> getFilters() { return filters; } @Deprecated public static void setOnLoadListen(LoadListen listen){ uploadAdapter = listen; } @Deprecated public static LoadListen getLoadListen() { return uploadAdapter; } //=============================================================== private final static List<CoreWSFilter> filterList = Collections.synchronizedList(new ArrayList<CoreWSFilter>()); /** * 添加Core fliter * @param filters void */ public static void addCoreWSFilter(CoreWSFilter... filters){ synchronized (filterList) { for(CoreWSFilter filter : filters) filterList.add(filter); } } /** * delete core filter * @param coreWsFilterId void */ public static void removeCoreWSFilter(int coreWsFilterId){ CoreWSFilter deleteFilter = null; for(CoreWSFilter f : filterList){ if(f.getId() == coreWsFilterId){ deleteFilter = f; break; } } if(null != deleteFilter){ synchronized (filterList) { filterList.remove(deleteFilter); } } } // public static void clearAllCoreWsFilter(){ // synchronized (filterList) { // filterList.clear(); // } // } /** * get all core filter * @return List<CoreWSFilter> */ public List<CoreWSFilter> getCoreWSFilters(){ return filterList; } //=============================================================== public static Application getApplication(){ synchronized(lock){ if(null == app){ app = new Application(); } } return app; } private List<IXMLParse> list; private IXMLNode dbXml,rmiXml,sysXml,smsXml,lbsXml; private List<IXMLNode> initServiceXml; public Application(){ init(); //init system // initSystem(); // initService // initService(); } public void initService() { List<IXMLNode> sss = getInitServiceProperties(); for(IXMLNode ss : sss){ // <service space="" class=""/> List<IXMLNode> s = ss.getChilds("service"); for(IXMLNode n : s){ String c = n.getAttribute("class"); if(null != c){ try { Class<?> cs = Class.forName(c, false, Thread.currentThread().getContextClassLoader()); if(InitService.class.isAssignableFrom(cs)){ InitService o; if(cs.isInterface()){ Service a = cs.getAnnotation(Service.class); if(null == a){ continue; } String impl = a.impl(); Class<?> i = Class.forName(impl, false, Thread.currentThread().getContextClassLoader()); if(null == i){ continue; } o = (InitService) i.newInstance(); }else{ o = (InitService) cs.newInstance(); } o.init(); } } catch (Exception e) { // e.printStackTrace(); Log.getLog(getClass()).w(e); continue; } } } } } public void initSystem() { IXMLNode system = getSystemProperties(); // <system> // <rmi open="true"> // <users> // <user username="test" password="123455" maxConnectionCount="200"> // <!-- approvedIP,允许访问的ip,不设置表示不限制 --> // <!-- <ip>10.2.12.2</ip> --> // </user> // </users> // <!-- <remote> // <id>1</id> // <ip>127.0.0.1</ip> // <port>9700</port> // <username>root</username> // <password>123455</password> // </remote> --> // </rmi> // </system> //rmi if(null == system){ return; } List<IXMLNode> rmis = system.getChilds("rmi"); if(null != rmis && rmis.size()==1){ initRmi(rmis.get(0)); } } public void initSystem2(){ IXMLNode system = getSystemProperties(); // <system> // <rmi open="true"> // <users> // <user username="test" password="123455" maxConnectionCount="200"> // <!-- approvedIP,允许访问的ip,不设置表示不限制 --> // <!-- <ip>10.2.12.2</ip> --> // </user> // </users> // <!-- <remote> // <id>1</id> // <ip>127.0.0.1</ip> // <port>9700</port> // <username>root</username> // <password>123455</password> // </remote> --> // </rmi> // </system> //rmi if(null == system){ return; } List<IXMLNode> rmis = system.getChilds("rmi"); if(null != rmis && rmis.size()==1){ initRmi2(rmis.get(0)); } } private void initRmi2(IXMLNode n) { String open = n.getAttribute("open"); String port= n.getAttribute("port"); String ip = n.getAttribute("ip"); if(null != open){ boolean b = Boolean.parseBoolean(open); if(b){ int p; if(null == port || port.isEmpty()){ p=9700; }else{ p = Integer.parseInt(port); } try { RmiService2.openRmi(ip, p); } catch (RemoteException e) { e.printStackTrace(); } } } List<IXMLNode> users = n.getChilds("users"); if(null != users && 1==users.size()){ List<IXMLNode> us = users.get(0).getChilds("user"); for(IXMLNode u : us){ remoteUser.add(new RemoteUser(u)); } } List<IXMLNode> remotes = n.getChilds("remote"); for(IXMLNode r : remotes){ remoteAddress.add(new RemoteAddress(r)); } } private void initRmi(IXMLNode n) { // <rmi open="true"> // <users> // <user username="test" password="123455" maxConnectionCount="200"> // <!-- approvedIP,允许访问的ip,不设置表示不限制 --> // <!-- <ip>10.2.12.2</ip> --> // </user> // </users> // <!-- <remote> // <id>1</id> // <ip>127.0.0.1</ip> // <port>9700</port> // <username>root</username> // <password>123455</password> // </remote> --> // </rmi> String open = n.getAttribute("open"); String port= n.getAttribute("port"); String ip = n.getAttribute("ip"); if(null != open){ boolean b = Boolean.parseBoolean(open); if(b){ int p; if(null == port || port.isEmpty()){ p=9700; }else{ p = Integer.parseInt(port); } try { RmiService.openRmi(ip, p); } catch (RemoteException e) { e.printStackTrace(); } // if(null == port){ // try { // RmiService.openRmi(); // } catch (RemoteException e) { // e.printStackTrace(); // } // }else{ // int p = Integer.parseInt(port); // try { // RmiService.openRmi(p); // } catch (RemoteException e) { // e.printStackTrace(); // } // } } } List<IXMLNode> users = n.getChilds("users"); if(null != users && 1==users.size()){ List<IXMLNode> us = users.get(0).getChilds("user"); for(IXMLNode u : us){ remoteUser.add(new RemoteUser(u)); } } List<IXMLNode> remotes = n.getChilds("remote"); for(IXMLNode r : remotes){ remoteAddress.add(new RemoteAddress(r)); } } public List<RemoteAddress> getRemoteAddress() { return remoteAddress; } public List<RemoteUser> getRemoteUser() { return remoteUser; } protected void init() { IReadXML read = new ReadXML(); try { list = read.readModXML(); } catch (IOException e) { e.printStackTrace(); } } public IXMLNode getDBProperties(){ if(null == dbXml){ dbXml = getXMLNodeBySettings("db"); } return dbXml; } private IXMLNode getXMLNodeBySettings(String nodeName) { for(IXMLParse p : list){ IXMLNode root = p.getRootNode(); if(root.getNodeName().equals("settings")){ List<IXMLNode> s = root.getChilds(nodeName); if(!s.isEmpty()){ return s.get(0); } } } return null; } public IXMLNode getSystemProperties(){ if(null == sysXml) sysXml = getXMLNodeBySettings("system"); return sysXml; } public IXMLNode getRmiProperties(){ if(null == rmiXml) { List<IXMLNode> childs = getSystemProperties().getChilds("rmi"); if(!childs.isEmpty()) rmiXml = childs.get(0); } return rmiXml; } public IXMLNode getSmsProperties(){ if(null == smsXml) smsXml = getXMLNodeBySettings("sms"); return smsXml; } public IXMLNode getLbsProperties(){ if(null == lbsXml) lbsXml = getXMLNodeBySettings("lbs"); return lbsXml; } public List<IXMLNode> getInitServiceProperties(){ String nodeName = "initService"; if(null == initServiceXml) { initServiceXml = new ArrayList<IXMLNode>(); for(IXMLParse p : list){ IXMLNode root = p.getRootNode(); if(root.getNodeName().equals("settings")){ List<IXMLNode> s = root.getChilds(nodeName); if(!s.isEmpty()){ initServiceXml.add(s.get(0)); } } } } return initServiceXml; } public RemoteServiceFactory getDefaultLbsFactory(){ if(null == lbsRemote){ IXMLNode p = getLbsProperties(); if(null == p){ return null; } // <lbs id="1" ip="127.0.0.1"> // <user username="test" password="123455"/> // </lbs> String ip = p.getAttribute("ip"); String port = p.getAttribute("port"); IXMLNode user = p.getChilds("user").get(0); // String username = user.getAttribute("username"); // String password = user.getAttribute("password"); RemoteUser ru = new RemoteUser(user); if(null == port){ lbsRemote = new RemoteServiceFactory(ip); }else{ int po = Integer.parseInt(port); lbsRemote = new RemoteServiceFactory(ip, po); } lbsRemote.setUsername(ru.getUsername()); lbsRemote.setPassword(ru.getPassword()); } return lbsRemote; } //======================================in class============================== public static class RemoteUser{ // <xs:element name="user"> // <xs:complexType> // <xs:sequence> // <xs:element ref="ip" minOccurs="0" maxOccurs="unbounded"/> // </xs:sequence> // <xs:attribute name="username" use="required" type="xs:string"/> // <xs:attribute name="password" use="required" type="xs:string"/> // <xs:attribute name="maxConnectionCount" type="xs:integer"/> // <!-- <xs:attribute name="approvedIP" type="ip" /> --> // </xs:complexType> // </xs:element> private String username; private String password; protected final List<String> lockIps = new ArrayList<String>(); private Integer maxConnectionCount; public RemoteUser(String username, String password) { this.username = username; this.password = password; } public RemoteUser(IXMLNode user) { this.username = user.getAttribute("username"); this.password = user.getAttribute("password"); String count = user.getAttribute("maxConnectionCount"); if(null != count){ this.maxConnectionCount = Integer.parseInt(count); } List<IXMLNode> ns = user.getChilds("ip"); for(IXMLNode n : ns){ lockIps.add(n.getText()); } } public RemoteUser() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public List<String> getLockIps() { return lockIps; } public boolean valiIp(String ip){ return lockIps.contains(ip); } public Integer getMaxConnectionCount() { return maxConnectionCount; } public void setMaxConnectionCount(Integer maxConnectionCount) { this.maxConnectionCount = maxConnectionCount; } } // public class RemoteAddress{ // <xs:element name="remote"> // <xs:complexType> // <xs:sequence> // <xs:element ref="id"/> // <xs:element ref="ip"/> // <xs:element ref="port"/> // <xs:element ref="username"/> // <xs:element ref="password"/> // </xs:sequence> // </xs:complexType> // </xs:element> private int id; private String ip; private int port; private String username; private String password; public RemoteAddress(IXMLNode r) { String ids = getStr(r.getChilds("id")); if(null != ids) this.id = Integer.parseInt(ids); this.ip = getStr(r.getChilds("ip")); String ports = getStr(r.getChilds("port")); if(null != ports) this.port = Integer.parseInt(ports); this.username = getStr(r.getChilds("username")); this.password = getStr(r.getChilds("password")); } private String getStr(List<IXMLNode> ns) { if(null != ns && ns.size()==1){ return ns.get(0).getText(); } return null; } public RemoteAddress() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } }