1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.tools
.development
.agent
;
6 import java
.lang
.instrument
.Instrumentation
;
7 import java
.lang
.reflect
.Method
;
8 import java
.net
.MalformedURLException
;
9 import java
.net
.URISyntaxException
;
11 import java
.net
.URLClassLoader
;
12 import java
.security
.AllPermission
;
13 import java
.security
.CodeSource
;
14 import java
.security
.PermissionCollection
;
15 import java
.util
.logging
.Level
;
16 import java
.util
.logging
.Logger
;
19 * An instrumentation agent for the dev_appserver. Implements whitelisting and any
20 * other facilities that require instrumentation.
23 public class AppEngineDevAgent
{
25 private static final String AGENT_IMPL
=
26 "com.google.appengine.tools.development.agent.impl.AgentImpl";
28 private static final String AGENT_IMPL_JAR
= "appengine-agentimpl.jar";
30 private static final String AGENT_DIR_PROP
= "appengine-agent-dir";
32 private static final Logger logger
= Logger
.getLogger(AppEngineDevAgent
.class.getName());
34 private static Object impl
;
36 public static void premain(String agentArgs
, Instrumentation inst
) {
38 URL agentImplLib
= findAgentImplLib();
39 URLClassLoader agentImplLoader
= new URLClassLoader(new URL
[] {agentImplLib
}) {
41 protected PermissionCollection
getPermissions(CodeSource codesource
) {
42 PermissionCollection perms
= super.getPermissions(codesource
);
43 perms
.add(new AllPermission());
48 Class
<?
> implClass
= agentImplLoader
.loadClass(AGENT_IMPL
);
49 Method getInstance
= implClass
.getMethod("getInstance");
50 impl
= getInstance
.invoke(null);
51 Method run
= implClass
.getMethod("run", Instrumentation
.class, boolean.class);
52 run
.invoke(impl
, inst
, treatRestrictedClassListViolationsAsErrors(agentArgs
));
53 } catch (Exception e
) {
54 logger
.log(Level
.SEVERE
, "Unable to load the App Engine dev agent. Security restrictions " +
55 "will not be completely emulated.", e
);
59 private static boolean treatRestrictedClassListViolationsAsErrors(String agentArgs
) {
60 return "treatRestrictedClassListViolationsAsErrors=true".equals(agentArgs
);
64 * Returns the agent implementation, which is loaded in its own ClassLoader.
65 * The Agent structurally conforms to the {@link Agent Agent} interface.
67 public static Object
getAgent() {
71 private static URL
findAgentImplLib() {
73 URL codeLocation
= AppEngineDevAgent
.class.getProtectionDomain().getCodeSource().getLocation();
76 String agentDirFromSysProps
= System
.getProperty(AGENT_DIR_PROP
);
77 if (agentDirFromSysProps
!= null) {
78 agentDir
= new File(agentDirFromSysProps
);
81 agentDir
= new File(codeLocation
.toURI());
82 } catch (URISyntaxException e
) {
83 agentDir
= new File(codeLocation
.getFile());
86 agentDir
= agentDir
.getParentFile();
88 if (!agentDir
.isDirectory()) {
89 throw new RuntimeException(
90 "Unable to find agent directory at " + agentDir
.getAbsolutePath());
93 File agentImplLib
= new File(agentDir
, AGENT_IMPL_JAR
);
95 if (!agentImplLib
.exists()) {
96 throw new RuntimeException("Unable to find " + AGENT_IMPL_JAR
+ " in " +
97 agentDir
.getAbsolutePath());
101 return agentImplLib
.toURI().toURL();
102 } catch (MalformedURLException e
) {
103 throw new RuntimeException("Unable to retrieve a URL for " + agentImplLib
.getAbsolutePath(),