KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > standalone > InstrumentationAdapter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.standalone;
23
24 import java.lang.instrument.ClassDefinition JavaDoc;
25 import java.lang.instrument.Instrumentation JavaDoc;
26 import java.lang.instrument.UnmodifiableClassException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29
30 import org.jboss.aop.instrument.HotSwapper;
31
32 /**
33  * This class is an adapter to <code>java.lang.instrument.Instrumentation
34  * </code> and is used to enable hot swap in JBoss AOP through Java 5 agents.
35  *
36  * @author Flavia Rainone
37  */

38 class InstrumentationAdapter implements HotSwapper
39 {
40    
41    private Instrumentation JavaDoc instrumentation; // delegate
42
private Collection JavaDoc<ClassDefinition JavaDoc> classDefinitions;
43    
44    /**
45     * Constructor.
46     * @param instrumentation
47     */

48    public InstrumentationAdapter(Instrumentation JavaDoc instrumentation)
49    {
50       this.instrumentation = instrumentation;
51       this.classDefinitions = new ArrayList JavaDoc<ClassDefinition JavaDoc>();
52    }
53
54    /**
55     * Register class' byte codes redefinitions.
56     * @param clazz the clazz to be redefined.
57     * @param classCode the new byte code implementation of <code>clazz</code>.
58     */

59    public synchronized void registerChange(Class JavaDoc clazz, byte[] classCode)
60    {
61       ClassDefinition JavaDoc classDef = new ClassDefinition JavaDoc(clazz, classCode);
62       this.classDefinitions.add(classDef);
63    }
64
65    /**
66     * Hot swaps previously registered changes.
67     * All classes' byte codes registered through <code>register</code>
68     * are redefined in the system only when <code>hotSwap</code> is invoked.
69     */

70    public synchronized void hotSwap()
71    {
72       ClassDefinition JavaDoc[] definitions = new ClassDefinition JavaDoc[this.classDefinitions.size()];
73       definitions = (ClassDefinition JavaDoc[]) this.classDefinitions.toArray(definitions);
74       try
75       {
76          instrumentation.redefineClasses(definitions);
77       }
78       catch(ClassNotFoundException JavaDoc e)
79       {
80          throw new RuntimeException JavaDoc(e);
81       }
82       catch(UnmodifiableClassException JavaDoc e)
83       {
84          throw new RuntimeException JavaDoc(e);
85       }
86       this.classDefinitions.clear();
87    }
88 }
Popular Tags