1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.android_scripting.interpreter; 18 19 import com.googlecode.android_scripting.AndroidProxy; 20 import com.googlecode.android_scripting.Log; 21 import com.googlecode.android_scripting.Process; 22 import com.googlecode.android_scripting.SimpleServer; 23 import com.googlecode.android_scripting.jsonrpc.RpcReceiverManagerFactory; 24 25 import java.net.InetSocketAddress; 26 import java.net.SocketException; 27 import java.net.UnknownHostException; 28 29 /** 30 * This is a skeletal implementation of an interpreter process. 31 * 32 * @author Damon Kohler (damonkohler@gmail.com) 33 */ 34 public class InterpreterProcess extends Process { 35 36 private final AndroidProxy mProxy; 37 private final Interpreter mInterpreter; 38 private String mCommand; 39 40 /** 41 * Creates a new {@link InterpreterProcess}. 42 * 43 * @param launchScript 44 * the absolute path to a script that should be launched with the interpreter 45 * @param port 46 * the port that the AndroidProxy is listening on 47 */ InterpreterProcess(Interpreter interpreter, AndroidProxy proxy)48 public InterpreterProcess(Interpreter interpreter, AndroidProxy proxy) { 49 mProxy = proxy; 50 mInterpreter = interpreter; 51 52 setBinary(interpreter.getBinary()); 53 setName(interpreter.getNiceName()); 54 setCommand(interpreter.getInteractiveCommand()); 55 addAllArguments(interpreter.getArguments()); 56 putAllEnvironmentVariables(System.getenv()); 57 putEnvironmentVariable("AP_HOST", getHost()); 58 putEnvironmentVariable("AP_PORT", Integer.toString(getPort())); 59 if (proxy.getSecret() != null) { 60 putEnvironmentVariable("AP_HANDSHAKE", getSecret()); 61 } 62 putAllEnvironmentVariables(interpreter.getEnvironmentVariables()); 63 } 64 setCommand(String command)65 protected void setCommand(String command) { 66 mCommand = command; 67 } 68 getInterpreter()69 public Interpreter getInterpreter() { 70 return mInterpreter; 71 } 72 getHost()73 public String getHost() { 74 String result = mProxy.getAddress().getHostName(); 75 if (result.equals("0.0.0.0")) { // Wildcard. 76 try { 77 return SimpleServer.getPublicInetAddress().getHostName(); 78 } catch (UnknownHostException e) { 79 Log.i("public address", e); 80 e.printStackTrace(); 81 } catch (SocketException e) { 82 Log.i("public address", e); 83 } 84 } 85 return result; 86 } 87 getPort()88 public int getPort() { 89 return mProxy.getAddress().getPort(); 90 } 91 getAddress()92 public InetSocketAddress getAddress() { 93 return mProxy.getAddress(); 94 } 95 getSecret()96 public String getSecret() { 97 return mProxy.getSecret(); 98 } 99 getRpcReceiverManagerFactory()100 public RpcReceiverManagerFactory getRpcReceiverManagerFactory() { 101 return mProxy.getRpcReceiverManagerFactory(); 102 } 103 104 @Override start(final Runnable shutdownHook)105 public void start(final Runnable shutdownHook) { 106 if (!mCommand.isEmpty()) { 107 addArgument(mCommand); 108 } 109 super.start(shutdownHook); 110 } 111 112 @Override kill()113 public void kill() { 114 super.kill(); 115 mProxy.shutdown(); 116 } 117 118 @Override getWorkingDirectory()119 public String getWorkingDirectory() { 120 return InterpreterConstants.SDCARD_SL4A_ROOT; 121 } 122 } 123