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.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  */
33 public class InterpreterProcess extends Process {
34 
35   private final AndroidProxy mProxy;
36   private final Interpreter mInterpreter;
37   private String mCommand;
38 
39   /**
40    * Creates a new {@link InterpreterProcess}.
41    *
42    * @param launchScript
43    *          the absolute path to a script that should be launched with the interpreter
44    * @param port
45    *          the port that the AndroidProxy is listening on
46    */
InterpreterProcess(Interpreter interpreter, AndroidProxy proxy)47   public InterpreterProcess(Interpreter interpreter, AndroidProxy proxy) {
48     mProxy = proxy;
49     mInterpreter = interpreter;
50 
51     setBinary(interpreter.getBinary());
52     setName(interpreter.getNiceName());
53     setCommand(interpreter.getInteractiveCommand());
54     addAllArguments(interpreter.getArguments());
55     putAllEnvironmentVariables(System.getenv());
56     putEnvironmentVariable("AP_HOST", getHost());
57     putEnvironmentVariable("AP_PORT", Integer.toString(getPort()));
58     if (proxy.getSecret() != null) {
59       putEnvironmentVariable("AP_HANDSHAKE", getSecret());
60     }
61     putAllEnvironmentVariables(interpreter.getEnvironmentVariables());
62   }
63 
setCommand(String command)64   protected void setCommand(String command) {
65     mCommand = command;
66   }
67 
getInterpreter()68   public Interpreter getInterpreter() {
69     return mInterpreter;
70   }
71 
getHost()72   public String getHost() {
73     String result = mProxy.getAddress().getHostName();
74     if (result.equals("0.0.0.0")) { // Wildcard.
75       try {
76         return SimpleServer.getPublicInetAddress().getHostName();
77       } catch (UnknownHostException e) {
78         Log.i("public address", e);
79         e.printStackTrace();
80       } catch (SocketException e) {
81         Log.i("public address", e);
82       }
83     }
84     return result;
85   }
86 
getPort()87   public int getPort() {
88     return mProxy.getAddress().getPort();
89   }
90 
getAddress()91   public InetSocketAddress getAddress() {
92     return mProxy.getAddress();
93   }
94 
getSecret()95   public String getSecret() {
96     return mProxy.getSecret();
97   }
98 
getRpcReceiverManagerFactory()99   public RpcReceiverManagerFactory getRpcReceiverManagerFactory() {
100     return mProxy.getRpcReceiverManagerFactory();
101   }
102 
103   @Override
start(final Runnable shutdownHook)104   public void start(final Runnable shutdownHook) {
105     if (!mCommand.isEmpty()) {
106       addArgument(mCommand);
107     }
108     super.start(shutdownHook);
109   }
110 
111   @Override
kill()112   public void kill() {
113     super.kill();
114     mProxy.shutdown();
115   }
116 
117   @Override
getWorkingDirectory()118   public String getWorkingDirectory() {
119     return InterpreterConstants.SDCARD_SL4A_ROOT;
120   }
121 }
122