Skip to main content
summaryrefslogtreecommitdiffstats
blob: 159f04f14250787713c4a76a34a4408207fcf079 (plain) (blame)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

package org.eclipse.osee.framework.derby;

import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.logging.Level;
import org.apache.derby.drda.NetworkServerControl;
import org.eclipse.osee.framework.derby.internal.Activator;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Roberto E. Escobar
 */
public class DerbyDbServer {
   protected static Object keepAlive;
   private NetworkServerUtil nwServer;
   private static final DerbyDbServer instance = new DerbyDbServer();

   private DerbyDbServer() {
      this.nwServer = null;
   }

   public static void startServer(String host, int port) throws Exception {
      instance.startServerInternal(host, port);
   }

   private void startServerInternal(String host, int port) throws Exception {
      try {
         OseeLog.log(Activator.class, Level.INFO, "Starting Derby Network Server ....");
         nwServer = new NetworkServerUtil(InetAddress.getByName(host), port);
         nwServer.start();

         if (isConnectionAvailable()) {
            nwServer.printInfo();
            addShutdownHook();
            //            stayAlive();
         } else {
            OseeLog.log(Activator.class, Level.INFO, "Exiting, since unable to connect to Derby Network Server.");
            OseeLog.log(Activator.class, Level.INFO,
                  "Please try to increase the amount of time to keep trying to connect to the Server.");
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

   private void stayAlive() {
      keepAlive = new Object();
      synchronized (keepAlive) {
         try {
            keepAlive.wait();
         } catch (InterruptedException e) {
            OseeLog.log(Activator.class, Level.SEVERE, e.getMessage(), e);
         }
      }
   }

   private void commitSuicide() {
      if (keepAlive != null) {
         synchronized (keepAlive) {
            keepAlive.notify();
         }
      }
   }

   private void shutdown() {
      OseeLog.log(Activator.class, Level.INFO, "Shutting down network server...");
      nwServer.shutdown();
      OseeLog.log(Activator.class, Level.INFO, "Server down.");
      commitSuicide();
   }

   private void addShutdownHook() {
      Runtime.getRuntime().addShutdownHook(new Thread() {
         @Override
         public void run() {
            OseeLog.log(Activator.class, Level.INFO, "Shutting down");
            shutdown();
         }
      });
   }

   private boolean isConnectionAvailable() throws InterruptedException {
      boolean knowIfServerUp = false;
      int numTimes = 5;

      while (!knowIfServerUp && numTimes > 0) {
         try {
            numTimes--;
            nwServer.testForConnection();
            knowIfServerUp = true;
         } catch (Exception e) {
            OseeLog.log(Activator.class, Level.SEVERE,
                  "Unable to obtain a connection to network server, trying again after 3000 ms.", e);
            Thread.sleep(3000);
         }
      }
      return knowIfServerUp;
   }

   // public static void main(String[] args) throws Exception {
   // (new DBServer( new PrintWriter(System.out, true)).startServer(args);
   // }

   private final class NetworkServerUtil {

      private NetworkServerControl serverControl;

      public NetworkServerUtil(InetAddress host, int port) {
         try {
            serverControl = new NetworkServerControl(host, port);
            OseeLog.log(Activator.class, Level.INFO, "Derby Network Server created");
         } catch (Exception e) {
            OseeLog.log(Activator.class, Level.SEVERE, "Error Initializing Server Control.", e);
         }
      }

      /**
       * trace utility of server
       */
      public void trace(boolean onoff) {
         try {
            serverControl.trace(onoff);
         } catch (Exception e) {
            OseeLog.log(Activator.class, Level.SEVERE, e.getMessage(), e);
         }
      }

      /**
       * Try to test for a connection Throws exception if unable to get a connection
       */
      public void testForConnection() throws Exception {
         serverControl.ping();
      }

      /**
       * Shutdown the NetworkServer
       */
      public void shutdown() {
         try {
            serverControl.shutdown();
         } catch (Exception e) {
            OseeLog.log(Activator.class, Level.SEVERE, e.getMessage(), e);
         }
      }

      /**
       * Start Derby Network server
       */
      public void start() {
         try {
            serverControl.start(new PrintWriter(System.out, true));
         } catch (Exception e) {
            OseeLog.log(Activator.class, Level.SEVERE, e.getMessage(), e);
         }
      }

      public void printInfo() {
         try {
            String sysinfo = serverControl.getSysinfo();
            int start = sysinfo.indexOf("Java classpath:");
            int stop = sysinfo.indexOf("OS name:");
            String cpSubString = sysinfo.substring(start, stop);
            cpSubString = cpSubString.replaceAll(";", ";\n\t");
            String temp = sysinfo.substring(0, start) + cpSubString + sysinfo.substring(stop, sysinfo.length());
            OseeLog.log(Activator.class, Level.INFO, temp);
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, "Error getting Server Information", ex);
         }
      }
   }
}

Back to the top