1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.layoutlib.bridge.remote.server; 18 19 import com.android.layout.remote.api.RemoteBridge; 20 import com.android.tools.layoutlib.annotations.NotNull; 21 22 import java.io.BufferedReader; 23 import java.io.IOException; 24 import java.io.InputStreamReader; 25 import java.lang.management.ManagementFactory; 26 import java.lang.management.RuntimeMXBean; 27 import java.nio.file.Path; 28 import java.nio.file.Paths; 29 import java.rmi.NoSuchObjectException; 30 import java.rmi.RemoteException; 31 import java.rmi.registry.LocateRegistry; 32 import java.rmi.registry.Registry; 33 import java.rmi.server.UnicastRemoteObject; 34 import java.util.ArrayList; 35 import java.util.List; 36 import java.util.concurrent.ArrayBlockingQueue; 37 import java.util.concurrent.BlockingQueue; 38 import java.util.concurrent.TimeUnit; 39 import java.util.stream.Collectors; 40 41 /** 42 * Main server class. The main method will start an RMI server for the {@link RemoteBridgeImpl} 43 * class. 44 */ 45 public class ServerMain { 46 private static final String RUNNING_SERVER_STR = "Server is running on port "; 47 public static int REGISTRY_BASE_PORT = 9000; 48 49 private final int mPort; 50 private final Registry mRegistry; 51 52 private ServerMain(int port, @NotNull Registry registry) { 53 mPort = port; 54 mRegistry = registry; 55 } 56 57 public int getPort() { 58 return mPort; 59 } 60 61 public void stop() { 62 try { 63 UnicastRemoteObject.unexportObject(mRegistry, true); 64 } catch (NoSuchObjectException ignored) { 65 } 66 } 67 68 /** 69 * This will start a new JVM and connect to the new JVM RMI registry. 70 * <p/> 71 * The server will start looking for ports available for the {@link Registry} until a free one 72 * is found. The port number will be returned. 73 * If no ports are available, a {@link RemoteException} will be thrown. 74 * @param basePort port number to start looking for available ports 75 * @param limit number of ports to check. The last port to be checked will be (basePort + limit) 76 */ 77 public static ServerMain forkAndStartServer(int basePort, int limit) 78 throws IOException, InterruptedException { 79 // We start a new VM by copying all the parameter that we received in the current VM. 80 // We only remove the agentlib parameter since that could cause a port collision and avoid 81 // the new VM from starting. 82 RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); 83 List<String> arguments = runtimeMxBean.getInputArguments().stream() 84 .filter(arg -> !arg.contains("-agentlib")) // Filter agentlib to avoid conflicts 85 .collect(Collectors.toList()); 86 87 Path javaPath = Paths.get(System.getProperty("java.home"), "bin", "java"); 88 String thisClassName = ServerMain.class.getName() 89 .replace('.','/'); 90 91 List<String> cmd = new ArrayList<>(); 92 cmd.add(javaPath.toString()); 93 94 // Inherited arguments 95 cmd.addAll(arguments); 96 97 // Classpath 98 cmd.add("-cp"); 99 cmd.add(System.getProperty("java.class.path")); 100 101 // Class name and path 102 cmd.add(thisClassName); 103 104 // ServerMain parameters [basePort. limit] 105 cmd.add(Integer.toString(basePort)); 106 cmd.add(Integer.toString(limit)); 107 108 Process process = new ProcessBuilder() 109 .command(cmd) 110 .start(); 111 112 BlockingQueue<String> outputQueue = new ArrayBlockingQueue<>(10); 113 Thread outputThread = new Thread(() -> { 114 BufferedReader inputStream = new BufferedReader( 115 new InputStreamReader(process.getInputStream())); 116 inputStream.lines() 117 .forEach(outputQueue::offer); 118 119 }); 120 outputThread.setName("output thread"); 121 outputThread.start(); 122 123 Runnable killServer = () -> { 124 process.destroyForcibly(); 125 outputThread.interrupt(); 126 try { 127 outputThread.join(); 128 } catch (InterruptedException ignore) { 129 } 130 }; 131 132 // Try to read the "Running on port" line in 10 lines. If it's not there just fail. 133 for (int i = 0; i < 10; i++) { 134 String line = outputQueue.poll(1000, TimeUnit.SECONDS); 135 136 if (line.startsWith(RUNNING_SERVER_STR)) { 137 int runningPort = Integer.parseInt(line.substring(RUNNING_SERVER_STR.length())); 138 System.out.println("Running on port " + runningPort); 139 140 // We already know where the server is running so we just need to get the registry 141 // and return our own instance of ServerMain 142 Registry registry = LocateRegistry.getRegistry(runningPort); 143 return new ServerMain(runningPort, registry) { 144 @Override 145 public void stop() { 146 killServer.run(); 147 } 148 }; 149 } 150 } 151 152 killServer.run(); 153 throw new IOException("Unable to find start string"); 154 } 155 156 /** 157 * The server will start looking for ports available for the {@link Registry} until a free one 158 * is found. The port number will be returned. 159 * If no ports are available, a {@link RemoteException} will be thrown. 160 * @param basePort port number to start looking for available ports 161 * @param limit number of ports to check. The last port to be checked will be (basePort + limit) 162 */ 163 private static ServerMain startServer(int basePort, int limit) throws RemoteException { 164 RemoteBridgeImpl remoteBridge = new RemoteBridgeImpl(); 165 RemoteBridge stub = (RemoteBridge) UnicastRemoteObject.exportObject(remoteBridge, 0); 166 167 RemoteException lastException = null; 168 for (int port = basePort; port <= basePort + limit; port++) { 169 try { 170 Registry registry = LocateRegistry.createRegistry(port); 171 registry.rebind(RemoteBridge.class.getName(), stub); 172 return new ServerMain(port, registry); 173 } catch (RemoteException e) { 174 lastException = e; 175 } 176 } 177 178 if (lastException == null) { 179 lastException = new RemoteException("Unable to start server"); 180 } 181 182 throw lastException; 183 } 184 185 public static void main(String[] args) throws RemoteException { 186 int basePort = args.length > 0 ? Integer.parseInt(args[0]) : REGISTRY_BASE_PORT; 187 int limit = args.length > 1 ? Integer.parseInt(args[1]) : 10; 188 189 ServerMain server = startServer(basePort, limit); 190 System.out.println(RUNNING_SERVER_STR + server.getPort()); 191 } 192 } 193