Skip to main content
summaryrefslogtreecommitdiffstats
blob: 89a96fde8462f54e52d3401adf67953e30a94434 (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
183
184
185
/*******************************************************************************
 * 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.jdk.core.util.xml;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.osee.framework.jdk.core.util.network.PortUtil;

/**
 * @author Roberto E. Escobar
 */
public class XmlTransformAsProcess {

   private Socket clientSocket;
   private InputStream inputFromNetwork;
   private OutputStream outputFromNetwork;
   private Process process;

   private XmlTransformAsProcess() {
      process = null;
      clientSocket = null;
      inputFromNetwork = null;
      outputFromNetwork = null;
   }

   private URL getClassLocation(final Class<XmlTransformServer> classToFind) {
      URL result = null;
      if (classToFind == null) {
         throw new IllegalArgumentException("Class is null");
      }
      final String classAsResource = classToFind.getName().replace('.', '/').concat(".class");
      final ProtectionDomain pd = classToFind.getProtectionDomain();
      if (pd != null) {
         final CodeSource cs = pd.getCodeSource();
         if (cs != null) {
            result = cs.getLocation();
         }
         if (result != null) {
            // Convert a code source location into a full class file location
            if (result.getProtocol().equals("file")) {
               try {
                  if (result.toExternalForm().endsWith(".jar") || result.toExternalForm().endsWith(".zip")) {
                     result = new URL("jar:".concat(result.toExternalForm()).concat("!/").concat(classAsResource));
                  } else if (new File(result.getFile()).isDirectory()) {
                     result = new URL(result, classAsResource);
                  }
               } catch (MalformedURLException ignore) {
                  // do nothing
               }
            }
         }
      }
      if (result == null) {
         // Try to find class definition as a resource
         final ClassLoader classLoader = classToFind.getClassLoader();
         result =
            classLoader != null ? classLoader.getResource(classAsResource) : ClassLoader.getSystemResource(classAsResource);
      }
      return result;
   }

   private void connectToServer(InetAddress address, int port) throws Exception {
      try {
         for (int i = 0; i < 10; i++) {
            try {
               clientSocket = new Socket(address, port);
               break;
            } catch (Throwable th) {
               Thread.sleep(1000);
            }
         }
         System.out.println("Connected");
         inputFromNetwork = new BufferedInputStream(clientSocket.getInputStream());
         outputFromNetwork = new BufferedOutputStream(clientSocket.getOutputStream());
      } catch (Exception ex) {
         throw new Exception("Unable to Connect to Transform Server. ", ex);
      }
   }

   private void launchServer(int port) throws Exception {
      List<String> commands = new ArrayList<String>();
      try {
         URL url = getClassLocation(XmlTransformServer.class);
         String path = new File(url.toURI()).getAbsolutePath();
         int indexOf = path.indexOf("bin");
         path = path.substring(0, indexOf + 4);
         File classFileLocation = new File(path);

         String className = XmlTransformServer.class.getName();

         commands.add("java");
         commands.add("-Xmx1024M");
         commands.add(className);
         commands.add(Integer.toString(port));

         ProcessBuilder builder = new ProcessBuilder();
         builder.directory(classFileLocation);
         builder.command(commands);
         process = builder.start();
         Thread.sleep(800);
      } catch (URISyntaxException ex) {
         throw new Exception("Unable to find XmlTransformServer class in File System. ", ex);
      } catch (Exception ex) {
         throw new Exception("Unable to launch TransformServer. ", ex);
      }
   }

   public void processXml(InputStream xmlSource, InputStream xsltSource, Writer result) {
      try {
         XmlTransformServer.sendStream(clientSocket, xmlSource, outputFromNetwork);
         Thread.sleep(2000);
         System.gc();
         System.out.println("Sent Xml");
         XmlTransformServer.sendStream(clientSocket, xsltSource, outputFromNetwork);
         System.gc();
         Thread.sleep(2000);
         System.out.println("Sent Xslt");
         XmlTransformServer.receiveStream(clientSocket, inputFromNetwork, result);
         System.gc();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }

   private void clearResources() throws Exception {
      if (inputFromNetwork != null) {
         inputFromNetwork.close();
      }
      if (outputFromNetwork != null) {
         outputFromNetwork.close();
      }
      if (clientSocket != null) {
         clientSocket.close();
      }
      if (process != null) {
         process.destroy();
      }
   }

   public static void getHtmlFromXml(InputStream xmlSource, InputStream xsltSource, Writer results) throws Exception {
      XmlTransformAsProcess xmlTransformProcess = new XmlTransformAsProcess();
      try {
         InetAddress address = InetAddress.getLocalHost();
         int port = PortUtil.getInstance().getValidPort();
         System.out.println("Transform Server at port: " + port);
         System.gc();
         xmlTransformProcess.launchServer(port);
         xmlTransformProcess.connectToServer(address, port);
         xmlTransformProcess.processXml(xmlSource, xsltSource, results);

      } finally {
         xmlTransformProcess.clearResources();
         System.gc();
      }
   }

   public static void main(String[] args) throws Exception {
      InputStream xmlInput = new FileInputStream(args[0]);
      InputStream xsltInput = new FileInputStream(args[1]);
      getHtmlFromXml(xmlInput, xsltInput, new PrintWriter(System.out));
   }
}

Back to the top