GrxServerManager.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 
00011 package com.generalrobotix.ui.util;
00012 
00013 import java.io.File;
00014 import java.io.IOException;
00015 import java.util.Vector;
00016 
00017 import org.eclipse.jface.preference.IPreferenceStore;
00018 import org.eclipse.ui.preferences.ScopedPreferenceStore;
00019 import org.eclipse.core.runtime.IProgressMonitor;
00020 
00021 import com.generalrobotix.ui.GrxBaseItem;
00022 import com.generalrobotix.ui.GrxPluginManager;
00023 import com.generalrobotix.ui.util.GrxXmlUtil;
00024 import com.generalrobotix.ui.util.GrxProcessManager.*;
00025 import com.generalrobotix.ui.grxui.Activator;
00026 import com.generalrobotix.ui.grxui.PreferenceConstants;
00027 import com.generalrobotix.ui.util.SynchronizedAccessor;
00028 
00029 @SuppressWarnings("serial")
00030 public class GrxServerManager extends GrxBaseItem{            
00031     private static volatile Vector<ProcessInfo> vecServerInfo          = new Vector<ProcessInfo>();
00032     private static volatile ProcessInfo         nameServerInfo = new ProcessInfo();
00033     private static SynchronizedAccessor<Integer>    newPort_ = new SynchronizedAccessor<Integer>(0);
00034     private static SynchronizedAccessor<String>    newHost_ = new SynchronizedAccessor<String>("");
00035     private static SynchronizedAccessor<String>    nameServerLogDir = new SynchronizedAccessor<String>("");
00036     private static final int    MAXMUM_PORT_NUMBER  = 65535;
00037     private static final String LINE_SEPARATOR = new String( System.getProperty("line.separator") );
00038     
00039     public static int     NAME_SERVER_PORT_ ;
00040     public static String  NAME_SERVER_HOST_ ;
00041     public static String  NAME_SERVER_LOG_DIR_ ;
00042     public String  serverInfoDefaultDir_  = "";
00043     public int     serverInfoDefaultWaitCount_ = 0;
00044 
00045     //次回起動のための値を保存
00046     public synchronized void SaveServerInfo() {
00047         setServerInfoToPreferenceStore();
00048         setNameServerInfoToPreferenceStore();
00049     }
00050     
00054         public GrxServerManager(String name, GrxPluginManager manager) {
00055                 super(name, manager);
00056     }
00057     
00062     public Vector<ProcessInfo> getServerInfo(){
00063         return vecServerInfo;
00064     }
00065 
00070     public ProcessInfo getNameServerInfo(){
00071         return nameServerInfo;
00072     }
00073     
00074     
00075     public String getNewHost(){
00076         return newHost_.get();
00077     }
00078     
00079     public int getNewPort(){
00080         return newPort_.get();
00081     }
00082     
00083     public String getNameserverLogDir(){
00084         return nameServerLogDir.get(); 
00085     }
00086       
00087     public String setNewHostPort(String host, String port,StringBuffer refHost, StringBuffer refPort){
00088         String retHost = checkHostFormat(host);
00089         String retPort = checkPortFormat(port);
00090         
00091         String ret = retHost + retPort;
00092         if( ret.isEmpty() ){
00093             newPort_.set(Integer.valueOf(port));
00094             newHost_.set(host);
00095         } else {
00096             if(!retHost.isEmpty()){
00097                 refHost.append( getNewHost() );
00098             }
00099             if(!retPort.isEmpty()){
00100                 refPort.append( Integer.toString(getNewPort()) );
00101             }
00102         }
00103         return ret;
00104     }
00105     
00114     public boolean toggleProcess(ProcessInfo pInfo){
00115         GrxProcessManager pm = (GrxProcessManager) manager_.getItem("processManager");
00116         AProcess process = pm.get(pInfo.id);
00117         StringBuffer nsHost = new StringBuffer("");
00118         StringBuffer nsPort = new StringBuffer("");
00119         Activator.refNSHostPort(nsHost, nsPort);
00120         String nsOpt = "-ORBInitRef NameService=corbaloc:iiop:" + nsHost +
00121             ":" + nsPort + "/NameService";
00122 
00123         if (process!=null){
00124                 if (process.isRunning()) {
00125                 // 停止処理
00126                 process.stop();
00127                 return false;
00128                 }else
00129                         pm.unregister(pInfo.id);
00130         }
00131         // 新規登録と開始処理
00132         String s = pInfo.com.get(pInfo.editComIndex);
00133         if(!(new File(s)).isAbsolute()){
00134                 pInfo.com.clear();
00135                 String ss = comToAbsolutePath(s);
00136                 if(ss!=null)
00137                         pInfo.com.add(ss);
00138                 pInfo.com.add(s);
00139         }
00140         if (pInfo.useORB) {
00141                 for(int i=0; i<pInfo.com.size(); i++)
00142                         pInfo.com.set(i, pInfo.com.get(i) + " " + nsOpt);
00143         }
00144         updatedParam(pInfo);
00145         pm.register(pInfo);
00146         if (!pm.get(pInfo.id).start(null)) {
00147                 // start 失敗
00148                 pm.unregister(pInfo.id);
00149                 return false;
00150         }
00151         
00152         return true;
00153     }
00154     
00161     private boolean updatedParam(ProcessInfo pInfo) {
00162         boolean ret = false;
00163         for (ProcessInfo pi : vecServerInfo) {
00164 
00165             if (pi.id.equals(pInfo.id)) {
00166                 if (pInfo.autoStart != pi.autoStart) {
00167                     pi.autoStart = pInfo.autoStart;
00168                     ret = true;
00169                 }
00170                 if (pInfo.useORB != pi.useORB) {
00171                     pi.useORB = pInfo.useORB;
00172                     ret = true;
00173                 }
00174                 if (!pInfo.com.equals(pi.com)) {
00175                     pi.com.clear();
00176                     for (String i : pInfo.com) {
00177                         pi.com.add(new String(i));
00178                     }
00179                     ret = true;
00180                 }
00181                 if (!pInfo.args.equals(pi.args)) {
00182                     pi.args = pInfo.args;
00183                     ret = true;
00184                 }
00185                 break;
00186             }
00187         }
00188 
00189         return ret;
00190     }
00191 
00192     private String checkHostFormat(String host){
00193         String ret = "";
00194         if(host.length() > 255){
00195             return MessageBundle.get("GrxServerManager.message.hostFormat.num") + LINE_SEPARATOR;
00196         }
00197         
00198         int limit = host.length() - host.replace(".", "").length();
00199         
00200         String [] splitStr = host.split("\\.", limit == 0 ? limit : limit + 1); 
00201         
00202         for(String i : splitStr){
00203             if(i.length() < 1 || i.length() > 63){
00204                 return MessageBundle.get("GrxServerManager.message.hostFormat.label") + LINE_SEPARATOR;
00205             }
00206             if( i.matches("^[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9]$") || 
00207                 i.matches("^[a-zA-Z0-9]$") ){
00208                 continue;
00209             } else {
00210                 return MessageBundle.get("GrxServerManager.message.hostFormat.rfc") + LINE_SEPARATOR;
00211             }
00212         }
00213         return ret;
00214     }
00215     
00216     private String checkPortFormat(String port){
00217         String ret = "";
00218         try{
00219             int portInt = Integer.valueOf(port); 
00220             if( portInt > MAXMUM_PORT_NUMBER || portInt < 0)
00221                 ret = MessageBundle.get("GrxServerManager.message.portFormat1") +
00222                         Integer.toString(MAXMUM_PORT_NUMBER) + MessageBundle.get("GrxServerManager.message.portFormat2") + LINE_SEPARATOR;
00223         }catch (NumberFormatException ex){
00224             ret = MessageBundle.get("GrxServerManager.message.portFormat1") +
00225                 Integer.toString(MAXMUM_PORT_NUMBER) + MessageBundle.get("GrxServerManager.message.portFormat2") + LINE_SEPARATOR;
00226         }
00227         return ret;
00228     }
00229 
00230     public void initialize(){
00231         getNameServerInfoFromPerferenceStore();
00232         vecServerInfo.clear();
00233         getServerInfoFromPerferenceStore();       
00234         newPort_.set(NAME_SERVER_PORT_);
00235         newHost_.set(NAME_SERVER_HOST_);
00236         nameServerLogDir.set(NAME_SERVER_LOG_DIR_);
00237     }
00238     
00239     public void restart(IProgressMonitor monitor){
00240         NAME_SERVER_PORT_ = newPort_.get();
00241         NAME_SERVER_HOST_ = newHost_.get();
00242         nameServerInfo.args = generateNameServerArgs();
00243         GrxProcessManager pm = (GrxProcessManager) manager_.getItem("processManager");
00244         pm.restart(monitor);
00245     }
00246     
00247     private String generateNameServerArgs(){
00248         String ret;
00249         ret = "-start " + Integer.toString(NAME_SERVER_PORT_) + " -logdir " + NAME_SERVER_LOG_DIR_ +
00250         " -ORBendPointPublish giop:tcp:"+ NAME_SERVER_HOST_ + ":";
00251         return ret;
00252     }
00253     private void getNameServerInfoFromPerferenceStore(){
00254         IPreferenceStore store =Activator.getDefault().getPreferenceStore();
00255         nameServerInfo.id = PreferenceConstants.NAMESERVER; 
00256         String _dir = store.getString(
00257                         PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.LOGGIR);
00258         NAME_SERVER_LOG_DIR_ = GrxXmlUtil.expandEnvVal(_dir).trim();       
00259         if(!NAME_SERVER_LOG_DIR_.isEmpty()){
00260             String localStr = NAME_SERVER_LOG_DIR_.replaceFirst("^\"", "");
00261             File logDir = new File(localStr.replaceFirst("\"$", ""));
00262             if(!logDir.exists()){
00263                 logDir.mkdirs();
00264             }
00265         }
00266         NAME_SERVER_PORT_ = store.getInt(
00267                         PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.PORT);
00268         if(NAME_SERVER_PORT_==0)
00269                 NAME_SERVER_PORT_ = store.getDefaultInt(
00270                         PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.PORT);
00271         NAME_SERVER_HOST_ = store.getString(
00272                                 PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.HOST);
00273         if(NAME_SERVER_HOST_.equals(""))
00274                 NAME_SERVER_HOST_ = store.getDefaultString(
00275                         PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.HOST);
00276         
00277         nameServerInfo.args = generateNameServerArgs();
00278         
00279         String s = store.getString(
00280                         PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.COM);
00281         //      優先度 SERVER_DIRの下, Pathの通っているところ, AbsolutePath //
00282         String ss=comToAbsolutePath(s);
00283         if(ss!=null)
00284                 nameServerInfo.com.add(ss);
00285         nameServerInfo.com.add(s);
00286         s = store.getString(
00287                         PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.COM+0);
00288         if(!s.equals(""))
00289                 nameServerInfo.com.add(s);
00290         
00291         nameServerInfo.autoStart = true;
00292         nameServerInfo.waitCount = store.getInt(
00293                                 PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.WAITCOUNT);
00294         nameServerInfo.dir = store.getString(
00295                         PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.DIR).trim();
00296         nameServerInfo.isCorbaServer = false;
00297         nameServerInfo.hasShutdown = store.getBoolean(
00298                         PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.HASSHUTDOWN);
00299         nameServerInfo.doKillall = false;
00300         nameServerInfo.autoStop = true;
00301     }
00302     
00303     private String[] parse(String string){
00304         String[] ret = string.split("\""+PreferenceConstants.SEPARATOR+"\"",-1);
00305         if(ret.length!=0){
00306                 if(ret[0].charAt(0)=='\"')
00307                         ret[0] = ret[0].substring(1,ret[0].length());
00308                 String s=ret[ret.length-1];
00309                 if(s.charAt(s.length()-1)=='\"')
00310                         ret[ret.length-1] = s.substring(0,s.length()-1);
00311         }
00312         return ret;
00313     }
00314     
00315     private void getServerInfoFromPerferenceStore(){
00316         IPreferenceStore store =Activator.getDefault().getPreferenceStore();
00317         String idList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.ID);
00318         String[] id = idList.split(PreferenceConstants.SEPARATOR,-1);
00319         String dirList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.DIR);
00320         String[] dir = parse(dirList);
00321         String waitCountList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.WAITCOUNT);
00322         String[] waitCount = waitCountList.split(PreferenceConstants.SEPARATOR,-1);
00323         String comList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.COM);
00324         String[] com = parse(comList);
00325         String argsList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.ARGS);
00326         String[] args = parse(argsList);
00327         String autoStartList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.AUTOSTART);
00328         String[] autoStart = autoStartList.split(PreferenceConstants.SEPARATOR,-1);
00329         String useORBList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.USEORB);
00330         String[] useORB = useORBList.split(PreferenceConstants.SEPARATOR,-1);
00331         String hasShutDownList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.HASSHUTDOWN);
00332         String[] hasShutDown = hasShutDownList.split(PreferenceConstants.SEPARATOR,-1);
00333         for(int i=1; i<id.length; i++){
00334                 ProcessInfo processInfo = new ProcessInfo();
00335                 processInfo.id = id[i];
00336                 String s=null;
00337                 if(i<com.length && !com[i].equals(""))
00338                         s = com[i].trim();
00339                 else
00340                         s = com[0].trim();
00341                 //  優先度 AbsolutePath, SERVER_DIRの下, Pathの通っているところ //
00342                 if((new File(s)).isAbsolute())
00343                         processInfo.com.add(s);
00344                 else{
00345                         String ss=comToAbsolutePath(s);
00346                         if(ss!=null){
00347                                 processInfo.com.add(ss);
00348                                 processInfo.editComIndex = 1;
00349                         }
00350                         processInfo.com.add(s);
00351                 }
00352                 if(i<args.length && !args[i].equals(""))
00353                         processInfo.args = args[i].trim();
00354                 else
00355                         processInfo.args = args[0].trim();
00356                 if(i<autoStart.length && !autoStart[i].equals(""))
00357                         processInfo.autoStart = autoStart[i].equals("true")? true : false;
00358                 else
00359                         processInfo.autoStart = autoStart[0].equals("true")? true : false;
00360                 if(i<useORB.length && !useORB[i].equals(""))
00361                         processInfo.useORB = useORB[i].equals("true")? true : false;
00362                 else
00363                         processInfo.useORB = useORB[0].equals("true")? true : false;
00364                 if(i<hasShutDown.length && !hasShutDown[i].equals(""))
00365                         processInfo.hasShutdown = hasShutDown[i].equals("true")? true : false;
00366                 else
00367                         processInfo.hasShutdown = hasShutDown[0].equals("true")? true : false;
00368                 if(i<waitCount.length && !waitCount[i].equals(""))
00369                         processInfo.waitCount = Integer.parseInt(waitCount[i]);
00370                 else
00371                         processInfo.waitCount = Integer.parseInt(waitCount[0]);
00372                 if(i<dir.length && !dir[i].equals(""))
00373                         processInfo.dir = dir[i].trim();
00374                 else
00375                         processInfo.dir = dir[0].trim();
00376                 processInfo.isCorbaServer = true;
00377                 processInfo.doKillall = false;
00378                 processInfo.autoStop = true;   
00379                 vecServerInfo.add(processInfo);
00380         }       
00381         serverInfoDefaultDir_ = dir[0].trim();
00382         serverInfoDefaultWaitCount_ = Integer.parseInt(waitCount[0]);
00383         
00384         if(nameServerInfo.waitCount==0)
00385                 nameServerInfo.waitCount = serverInfoDefaultWaitCount_;
00386         if(nameServerInfo.dir.equals(""))
00387                 nameServerInfo.dir = serverInfoDefaultDir_;
00388     }
00389     
00390     private String comToAbsolutePath(String com){
00391         if((new File(com)).isAbsolute())
00392                 return null;
00393         IPreferenceStore store =Activator.getDefault().getPreferenceStore();
00394         String serverDir = store.getString("SERVER_DIR");
00395         if(!serverDir.equals("")){
00396                 File file = new File(serverDir);
00397                 String[] list = file.list();
00398                 for(int i=0; i<list.length; i++){
00399                         int endIndex = list[i].lastIndexOf(".");
00400                         if(endIndex<0)
00401                                 endIndex = list[i].length();
00402                         String s=list[i].substring(0, endIndex);
00403                         if(s.equals(com)){
00404                                 return serverDir+"/"+com;
00405                         }
00406                 }
00407         }
00408         return null;
00409     }
00410     
00411     public static void setServerInfoToPreferenceStore(){
00412         IPreferenceStore store =Activator.getDefault().getPreferenceStore();
00413         String id = PreferenceConstants.ALLSERVER;
00414         String com = "\"\"";
00415         String args = "\"\"";
00416         String autoStart= "false";
00417         String useORB= "false";
00418         for(int i=0; i<vecServerInfo.size(); i++){
00419                 ProcessInfo processInfo =vecServerInfo.elementAt(i);
00420                 id += PreferenceConstants.SEPARATOR + processInfo.id;
00421                 com += PreferenceConstants.SEPARATOR + "\""+processInfo.com.get(processInfo.editComIndex)+"\"";
00422                 args += PreferenceConstants.SEPARATOR + "\""+processInfo.args+"\"";
00423                 autoStart += PreferenceConstants.SEPARATOR + (processInfo.autoStart? "true" : "false");
00424                 useORB += PreferenceConstants.SEPARATOR + (processInfo.useORB? "true" : "false");
00425         }
00426         store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.ID, id);
00427         store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.COM, com);
00428         store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.ARGS, args);
00429         store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.AUTOSTART, autoStart);
00430         store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.USEORB, useORB);
00431     }
00432     
00433     public static void setNameServerInfoToPreferenceStore(){
00434         IPreferenceStore store =Activator.getDefault().getPreferenceStore();
00435         store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.PORT, 
00436                         newPort_.get());
00437         store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.HOST, 
00438                         newHost_.get());
00439     }
00440     
00441     public void restoreDefault(){
00442         IPreferenceStore store =Activator.getDefault().getPreferenceStore();
00443         store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.PORT);
00444         store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.NAMESERVER+"."+PreferenceConstants.HOST);
00445         store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.ID);
00446         store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.COM);
00447         store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.ARGS);
00448         store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.AUTOSTART);
00449         store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.USEORB);
00450         initialize();
00451     }
00452 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:54