KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > testsupport > MyMerlin


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.repository.testsupport;
17
18 import junit.framework.TestCase;
19
20 import org.apache.avalon.repository.provider.Builder;
21 import org.apache.avalon.repository.provider.Factory;
22 import org.apache.avalon.repository.provider.InitialContextFactory;
23 import org.apache.avalon.repository.provider.InitialContext;
24 import org.apache.avalon.repository.main.DefaultInitialContextFactory;
25 import org.apache.avalon.util.exception.ExceptionHelper;
26 import org.apache.avalon.util.env.Env;
27 import java.io.File JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 import org.apache.avalon.repository.Artifact;
32
33 /**
34  * Allows to start/stop Merlin and get access components from it.
35  */

36 public class MyMerlin extends TestCase {
37     private Object JavaDoc m_kernel;
38
39     private Object JavaDoc m_root;
40
41     private Method JavaDoc m_locate;
42     private Method JavaDoc m_resolve;
43     private Method JavaDoc m_shutdown;
44
45     private static final String JavaDoc DEPLOYMENT_MODEL_CLASSNAME =
46       "org.apache.avalon.composition.model.DeploymentModel";
47
48     private TestSupportConfig config;
49
50     public MyMerlin(TestSupportConfig config) {
51         this.config = config;
52     }
53
54     public void startMerlin() throws Exception JavaDoc {
55
56         // *** START COPY FROM MERLIN MAIN SAMPLE ****
57

58         //
59
// Create the initial context factory. This establishes
60
// the application group from which properties will
61
// be resolved. It also provides operations supporting
62
// customization of the application environment.
63
//
64

65         InitialContextFactory initial =
66           new DefaultInitialContextFactory( "merlin" );
67         initial.setCacheDirectory(new File JavaDoc(config.getRequiredProperty("testsupport.merlinsystem")));
68         InitialContext context = initial.createInitialContext();
69
70         //
71
// Using the initial context we can now load any repository
72
// application using an artifact specification. Meta
73
// information associated with the artifact is used to
74
// construct the classloader that the application needs in
75
// order to execute.
76
//
77

78         String JavaDoc spec = config.getRequiredProperty("testsupport.merlinspec"); // something like "artifact:merlin/merlin-impl#3.3-SNAPSHOT";
79
Artifact artifact = Artifact.createArtifact( spec );
80         Builder builder = context.newBuilder( artifact );
81
82
83         //
84
// With the classloader established we can go ahead and
85
// and get the application factory. The factory has already
86
// been parameterized with defaults derived from properties
87
// based on the application group. We can provide
88
// overriding values by setting the factory criteria to
89
// application specific values following which we instantiate
90
// the application.
91
//
92

93         Factory factory = builder.getFactory();
94         Map JavaDoc criteria = factory.createDefaultCriteria();
95
96         // *** END COPY FROM MERLIN MAIN SAMPLE ****
97

98         // The rest of the code is inspired from the AbstractMerlinTestCase
99
// The reason some operations are done with reflection is to avoid
100
// having to put the dependencies for these classes in the project.xml,
101
// because otherwise we would have to put all merlin's dependencies in
102
// there, cause otherwise classes would not be found because of the
103
// way the classloader lookup mechanism works
104

105         // Use the same repository as Maven
106
File JavaDoc repository = new File JavaDoc(getMavenHome(), "repository");
107         criteria.put("merlin.repository", repository);
108
109         m_kernel = factory.create( criteria );
110
111         m_shutdown = m_kernel.getClass().getMethod("shutdown", new Class JavaDoc[0]);
112         Method JavaDoc method = m_kernel.getClass().getMethod("getModel", new Class JavaDoc[0]);
113         m_root = method.invoke( m_kernel, new Object JavaDoc[0] );
114         m_locate = m_root.getClass().getMethod("getModel", new Class JavaDoc[]{ String JavaDoc.class });
115         Class JavaDoc modelClass = builder.getClassLoader().loadClass( DEPLOYMENT_MODEL_CLASSNAME );
116         m_resolve = modelClass.getMethod( "resolve", new Class JavaDoc[0] );
117     }
118
119     public void stopMerlin() throws Exception JavaDoc {
120         if (m_kernel == null)
121             throw new Exception JavaDoc("Merlin not started.");
122
123         m_shutdown.invoke(m_kernel, new Object JavaDoc[0]);
124         m_kernel = null;
125     }
126
127     public Object JavaDoc resolve(String JavaDoc path) throws Exception JavaDoc {
128         if (m_kernel == null)
129             throw new Exception JavaDoc("Merlin not started.");
130
131         Object JavaDoc model = m_locate.invoke( m_root, new Object JavaDoc[]{ path } );
132         return m_resolve.invoke( model, new Object JavaDoc[0] );
133     }
134
135     private static File JavaDoc getMavenHome()
136     {
137         try
138         {
139             String JavaDoc local =
140               System.getProperty(
141                 "maven.home.local",
142                 Env.getEnvVariable( "MAVEN_HOME_LOCAL" ) );
143
144             if( null != local ) return new File JavaDoc( local ).getCanonicalFile();
145
146             return new File JavaDoc(
147               System.getProperty( "user.home" )
148               + File.separator
149               + ".maven" ).getCanonicalFile();
150
151         }
152         catch( Throwable JavaDoc e )
153         {
154             final String JavaDoc error =
155               "Internal error while attempting to access environment.";
156             final String JavaDoc message =
157               ExceptionHelper.packException( error, e, true );
158             throw new RuntimeException JavaDoc( message );
159         }
160     }
161
162
163 }
164
Popular Tags