KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > tcp > CmpTcpServer


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13 /*
14  * This file is part of the QuickServer library
15  * Copyright (C) 2003-2005 QuickServer.org
16  *
17  * Use, modification, copying and distribution of this software is subject to
18  * the terms and conditions of the GNU Lesser General Public License.
19  * You should have received a copy of the GNU LGP License along with this
20  * library; if not, you can download a copy from <http://www.quickserver.org/>.
21  *
22  * For questions, suggestions, bug-reports, enhancement-requests etc.
23  * visit http://www.quickserver.org
24  *
25  */

26
27 package org.ejbca.ui.tcp;
28
29 import java.io.File JavaDoc;
30 import java.net.UnknownHostException JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.logging.FileHandler JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 import org.apache.commons.lang.StringUtils;
37 import org.quickserver.net.AppException;
38 import org.quickserver.net.server.QuickServer;
39
40 /**
41  * Starts and stops the CMP TCP listener service
42  */

43 public class CmpTcpServer {
44
45     public static String JavaDoc VER = "1.0";
46     
47     public static QuickServer myServer = null;
48     
49     public static void init(Properties JavaDoc prop) {
50         CmpTcpConfiguration.instance().init(prop);
51     }
52     
53     public static void start() throws UnknownHostException JavaDoc {
54         String JavaDoc cmdHandle = "org.ejbca.ui.tcp.CmpTcpCommandHandler";
55         String JavaDoc auth = null;
56
57         myServer = new QuickServer();
58         myServer.setClientAuthenticationHandler(auth);
59         myServer.setBindAddr(CmpTcpConfiguration.instance().getBindHost());
60         myServer.setPort(CmpTcpConfiguration.instance().getPort());
61         myServer.setName("CMP TCP Server v " + VER);
62         if(getQuickServerVersion() >= 1.2) {
63             System.out.println("Using 1.2 feature");
64             myServer.setClientBinaryHandler(cmdHandle);
65             myServer.setClientEventHandler(cmdHandle);
66
67             //reduce info to Console
68
myServer.setConsoleLoggingToMicro();
69         }
70
71         //setup logger to log to file
72
Logger JavaDoc logger = null;
73         FileHandler JavaDoc txtLog = null;
74         String JavaDoc logDir = CmpTcpConfiguration.instance().getLogDir();
75         File JavaDoc log = new File JavaDoc(logDir + "/");
76         if(!log.canRead())
77             log.mkdir();
78         try {
79             logger = Logger.getLogger("");
80             logger.setLevel(Level.FINEST);
81
82             logger = Logger.getLogger("cmptcpserver");
83             logger.setLevel(Level.FINEST);
84             txtLog = new FileHandler JavaDoc(logDir+"/cmptcpserver.log");
85             //reduce info
86
txtLog.setFormatter(new org.quickserver.util.logging.MicroFormatter());
87             logger.addHandler(txtLog);
88
89             myServer.setAppLogger(logger); //imp
90

91             //myServer.setConsoleLoggingToMicro();
92
myServer.setConsoleLoggingFormatter("org.quickserver.util.logging.SimpleTextFormatter");
93             myServer.setConsoleLoggingLevel(Level.INFO);
94         } catch(Exception JavaDoc e){
95             System.err.println("Could not create xmlLog FileHandler : "+e);
96         }
97         //end of logger code
98

99         try {
100             String JavaDoc confFile = CmpTcpConfiguration.instance().getConfFile();
101             if (!StringUtils.isEmpty(confFile)) {
102                 Object JavaDoc config[] = new Object JavaDoc[] {confFile};
103                 if (myServer.initService(config) == false) {
104                     System.out.println("Configuration from config file "+confFile+" failed!");
105                 }
106             }
107
108             myServer.startServer();
109             //myServer.getQSAdminServer().setShellEnable(true);
110
//myServer.startQSAdminServer();
111
} catch(AppException e){
112             System.out.println("Error in server : "+e);
113             e.printStackTrace();
114         }
115     }
116
117     public static void stop() {
118         if (myServer != null) {
119             try {
120                 myServer.stopServer();
121             } catch (AppException e) {
122                 System.out.println("Error in server : "+e);
123                 e.printStackTrace();
124             }
125         }
126     }
127     public static float getQuickServerVersion() {
128         return QuickServer.getVersionNo();
129     }
130 }
131
132
133
Popular Tags