~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/pool/sizeof/AgentLoader.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 *  Copyright 2003-2010 Terracotta, Inc.
 
2
 *  Copyright Terracotta, Inc.
3
3
 *
4
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
5
 *  you may not use this file except in compliance with the License.
125
125
     * @return true if agent was loaded (which could have happened thought the -javaagent switch)
126
126
     */
127
127
    static boolean loadAgent() {
128
 
        if (!agentIsAvailable() && VIRTUAL_MACHINE_LOAD_AGENT != null) {
129
 
            try {
130
 
                String name = ManagementFactory.getRuntimeMXBean().getName();
131
 
                Object vm = VIRTUAL_MACHINE_ATTACH.invoke(null, name.substring(0, name.indexOf('@')));
 
128
        synchronized (AgentLoader.class.getName().intern()) {
 
129
            if (!agentIsAvailable() && VIRTUAL_MACHINE_LOAD_AGENT != null) {
132
130
                try {
133
 
                    File agent = getAgentFile();
134
 
                    LOGGER.info("Trying to load agent @ {}", agent);
135
 
                    if (agent != null) {
136
 
                        VIRTUAL_MACHINE_LOAD_AGENT.invoke(vm, agent.getAbsolutePath());
 
131
                    warnIfOSX();
 
132
                    String name = ManagementFactory.getRuntimeMXBean().getName();
 
133
                    Object vm = VIRTUAL_MACHINE_ATTACH.invoke(null, name.substring(0, name.indexOf('@')));
 
134
                    try {
 
135
                        File agent = getAgentFile();
 
136
                        LOGGER.info("Trying to load agent @ {}", agent);
 
137
                        if (agent != null) {
 
138
                            VIRTUAL_MACHINE_LOAD_AGENT.invoke(vm, agent.getAbsolutePath());
 
139
                        }
 
140
                    } finally {
 
141
                        VIRTUAL_MACHINE_DETACH.invoke(vm);
137
142
                    }
138
 
                } finally {
139
 
                    VIRTUAL_MACHINE_DETACH.invoke(vm);
 
143
                } catch (Throwable e) {
 
144
                    LOGGER.info("Failed to attach to VM and load the agent: {}: {}", e.getClass(), e.getMessage());
140
145
                }
141
 
            } catch (Throwable e) {
142
 
                LOGGER.info("Failed to attach to VM and load the agent: {}: {}", e.getClass(), e.getMessage());
143
146
            }
144
 
        }
145
 
 
146
 
        return agentIsAvailable();
 
147
 
 
148
            return agentIsAvailable();
 
149
        }
 
150
    }
 
151
 
 
152
    private static void warnIfOSX() {
 
153
        if (JvmInformation.isOSX() && System.getProperty("java.io.tmpdir") != null) {
 
154
            LOGGER.warn("Loading the SizeOfAgent will probably fail, as you are running on Apple OS X and have a value set for java.io.tmpdir\n" +
 
155
                        "They both result in a bug, not yet fixed by Apple, that won't let us attach to the VM and load the agent.\n" +
 
156
                        "Most probably, you'll also get a full thread-dump after this because of the failure... Nothing to worry about!\n" +
 
157
                        "You can bypass trying to load the Agent entirely by setting the System property '"
 
158
                        + net.sf.ehcache.pool.sizeof.AgentSizeOf.BYPASS_LOADING + "'  to true");
 
159
        }
147
160
    }
148
161
 
149
162
    private static File getAgentFile() throws IOException {
197
210
                instrumentation = (Instrumentation)getInstrumentationMethod.invoke(sizeOfAgentClass);
198
211
            }
199
212
            return instrumentation != null;
 
213
        } catch (SecurityException e) {
 
214
            LOGGER.warn("Couldn't access the system classloader because of the security policies applied by " +
 
215
                    "the security manager. You either want to loosen these, so ClassLoader.getSystemClassLoader() and " +
 
216
                    "reflection API calls are permitted or the sizing will be done using some other mechanism.\n" +
 
217
                    "Alternatively, set the system property net.sf.ehcache.sizeof.agent.instrumentationSystemProperty to true " +
 
218
                    "to have the agent put the required instances in the System Properties for the loader to access.");
 
219
            return false;
200
220
        } catch (Throwable e) {
201
221
            return false;
202
222
        }