1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.tools
.development
.agent
;
5 import static com
.google
.apphosting
.utils
.clearcast
.ClearCast
.cast
;
6 import static com
.google
.apphosting
.utils
.clearcast
.ClearCast
.staticCast
;
9 import java
.lang
.instrument
.Instrumentation
;
10 import java
.net
.MalformedURLException
;
11 import java
.net
.URISyntaxException
;
13 import java
.net
.URLClassLoader
;
14 import java
.util
.logging
.Level
;
15 import java
.util
.logging
.Logger
;
16 import java
.security
.PermissionCollection
;
17 import java
.security
.CodeSource
;
18 import java
.security
.AllPermission
;
21 * An instrumentation agent for the dev_appserver. Implements whitelisting and any
22 * other facilities that require instrumentation.
25 public class AppEngineDevAgent
{
27 private static final String AGENT_IMPL
=
28 "com.google.appengine.tools.development.agent.impl.AgentImpl";
30 private static final String AGENT_IMPL_JAR
= "appengine-agentimpl.jar";
32 private static final String AGENT_DIR_PROP
= "appengine-agent-dir";
34 private static final Logger logger
= Logger
.getLogger(AppEngineDevAgent
.class.getName());
36 private static Object impl
;
38 interface AgentImplStruct
{
39 void run(Instrumentation instrumentation
, boolean treatRestrictedClassListViolationsAsErrors
); Object
getInstance();
42 public static void premain(String agentArgs
, Instrumentation inst
) {
44 URL agentImplLib
= findAgentImplLib();
45 URLClassLoader agentImplLoader
= new URLClassLoader(new URL
[] {agentImplLib
}) {
47 protected PermissionCollection
getPermissions(CodeSource codesource
) {
48 PermissionCollection perms
= super.getPermissions(codesource
);
49 perms
.add(new AllPermission());
54 Class
<?
> implClass
= agentImplLoader
.loadClass(AGENT_IMPL
);
55 impl
= staticCast(implClass
, AgentImplStruct
.class).getInstance();
56 AgentImplStruct agentImplStruct
= cast(impl
, AgentImplStruct
.class);
57 agentImplStruct
.run(inst
, treatRestrictedClassListViolationsAsErrors(agentArgs
));
58 } catch (Exception e
) {
59 logger
.log(Level
.SEVERE
, "Unable to load the App Engine dev agent. Security restrictions " +
60 "will not be completely emulated.", e
);
64 private static boolean treatRestrictedClassListViolationsAsErrors(String agentArgs
) {
65 return "treatRestrictedClassListViolationsAsErrors=true".equals(agentArgs
);
69 * Returns the agent implementation, which is loaded in its own ClassLoader.
70 * The Agent structurally conforms to the {@link Agent Agent} interface.
72 public static Object
getAgent() {
76 private static URL
findAgentImplLib() {
78 URL codeLocation
= AppEngineDevAgent
.class.getProtectionDomain().getCodeSource().getLocation();
81 String agentDirFromSysProps
= System
.getProperty(AGENT_DIR_PROP
);
82 if (agentDirFromSysProps
!= null) {
83 agentDir
= new File(agentDirFromSysProps
);
86 agentDir
= new File(codeLocation
.toURI());
87 } catch (URISyntaxException e
) {
88 agentDir
= new File(codeLocation
.getFile());
91 agentDir
= agentDir
.getParentFile();
93 if (!agentDir
.isDirectory()) {
94 throw new RuntimeException(
95 "Unable to find agent directory at " + agentDir
.getAbsolutePath());
98 File agentImplLib
= new File(agentDir
, AGENT_IMPL_JAR
);
100 if (!agentImplLib
.exists()) {
101 throw new RuntimeException("Unable to find " + AGENT_IMPL_JAR
+ " in " +
102 agentDir
.getAbsolutePath());
106 return agentImplLib
.toURI().toURL();
107 } catch (MalformedURLException e
) {
108 throw new RuntimeException("Unable to retrieve a URL for " + agentImplLib
.getAbsolutePath(),