KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > main > DefaultInitialContextFactoryTestCase


1 /*
2  * Copyright 2004 The Apache Software Foundation
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
17 package org.apache.avalon.repository.main;
18
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import junit.framework.TestCase ;
25
26 import org.apache.avalon.repository.Artifact;
27 import org.apache.avalon.repository.Repository;
28 import org.apache.avalon.repository.provider.Factory;
29 import org.apache.avalon.repository.provider.Builder;
30 import org.apache.avalon.repository.provider.InitialContext;
31 import org.apache.avalon.repository.provider.InitialContextFactory;
32 import org.apache.avalon.util.exception.ExceptionHelper;
33 import org.apache.avalon.util.env.Env;
34
35 import org.apache.avalon.framework.configuration.Configuration;
36 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
37
38 /**
39  * DefaultInitialContextFactoryTestCase
40  *
41  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
42  * @version $Revision: 1.6 $
43  */

44 public class DefaultInitialContextFactoryTestCase extends TestCase
45 {
46     private static final String JavaDoc KEY = "merlin";
47
48     private static final File JavaDoc BASEDIR =
49       new File JavaDoc( System.getProperty( "basedir" ) );
50
51     public void testInitialContextCreation() throws Exception JavaDoc
52     {
53         DefaultInitialContextFactory factory =
54           new DefaultInitialContextFactory( KEY, BASEDIR );
55
56         //
57
// Before creating the intial context we can set the
58
// hosts, cache, parent classloader (and the repoitory
59
// implementation artifact but that would be unusual).
60
//
61
// Before modifying anything lets just check what the
62
// default values are.
63
//
64

65         System.out.println( "" );
66         System.out.println( "InitialContextFactory" );
67         System.out.println( "---------------------" );
68         System.out.println( " key: " + factory.getApplicationKey() );
69         System.out.println( " home: " + factory.getHomeDirectory() );
70         System.out.println( " cache: " + factory.getCacheDirectory() );
71         System.out.println( " work: " + factory.getWorkingDirectory() );
72         System.out.println( " impl: " + factory.getImplementation() );
73         String JavaDoc[] hosts = factory.getHosts();
74         for( int i=0; i<hosts.length; i++ )
75         {
76             System.out.println(
77               " host (" + (i+1) + "): "
78               + hosts[i] );
79         }
80         System.out.println( "" );
81
82         //
83
// Normally the default values should be ok and you should
84
// not need to modify anything, however, you have available
85
// serveral set methods on the InitialContextFactory interface
86
// that allow customization of the factory.
87

88         // In this example
89
// we will override the system cach with the local maven
90
// repository.
91
//
92

93         File JavaDoc repo = getMavenRepositoryDirectory();
94         factory.setCacheDirectory( repo );
95
96         //
97
// These actions declare factory artifacts to the initial context
98
// so that other applications don't have to go hunting.
99
//
100

101         Artifact[] artifacts =
102           getArtifactsToRegister( "src/test/conf/system.xml" );
103         factory.setFactoryArtifacts( artifacts );
104
105         //
106
// Once customized we can proceed with the instantiation
107
// of the initial context. The following method invocation
108
// will trigger the population of the system cache with the
109
// resources necesary to load the repository implementation.
110
//
111

112         InitialContext context = factory.createInitialContext();
113
114         //
115
// The following code simply prints out the working and cache
116
// directory, and the set of initial hosts assigned to the
117
// initial context.
118
//
119

120         System.out.println( "InitialContext" );
121         System.out.println( "--------------" );
122         System.out.println( " work: " + context.getInitialWorkingDirectory() );
123         System.out.println( " cache: " + context.getInitialCacheDirectory() );
124         hosts = context.getInitialHosts();
125         for( int i=0; i<hosts.length; i++ )
126         {
127             System.out.println(
128               " host (" + (i+1) + "): "
129               + hosts[i] );
130         }
131         System.out.println( "" );
132
133         //
134
// With the establishment of an initial context we can load any
135
// factory based system (e.g. logging, merlin, etc.). The following code
136
// requests an artifact for the repository implementation:
137
//
138

139         System.out.println( "Usage Example" );
140         System.out.println( "-------------" );
141         String JavaDoc key = Repository.class.getName();
142         System.out.println( " key: " + key );
143         Artifact[] candidates =
144           context.getRepository().getCandidates( Repository.class );
145         for( int i=0; i<candidates.length; i++ )
146         {
147             System.out.println( " artifact: " + candidates[i] );
148         }
149
150         //
151
// We can use the initial context to construct a plugin using a
152
// builder. The following example demonstrates the creation of a
153
// a repository plugin (the same process applies for merlin, logging
154
// plugins, runtime plugins, etc.).
155
//
156

157         if( candidates.length > 0 )
158         {
159             Builder builder = context.newBuilder( candidates[0] );
160             Factory exampleFactory = builder.getFactory();
161             Map JavaDoc criteria = exampleFactory.createDefaultCriteria();
162             Repository exampleRepository = (Repository) exampleFactory.create( criteria );
163             System.out.println( " instance: " + exampleRepository );
164             System.out.println( "" );
165         }
166     }
167
168     private Artifact[] getArtifactsToRegister( String JavaDoc path ) throws Exception JavaDoc
169     {
170         Configuration config =
171           getConfiguration( new File JavaDoc( BASEDIR, path ) );
172         Configuration[] children =
173           config.getChildren( "artifact" );
174         Artifact[] artifacts = new Artifact[ children.length ];
175         for( int i=0; i<children.length; i++ )
176         {
177             Configuration child = children[i];
178             String JavaDoc spec = child.getAttribute( "spec" );
179             Artifact artifact = Artifact.createArtifact( "artifact:" + spec );
180             artifacts[i] = artifact;
181         }
182         return artifacts;
183     }
184
185     private static File JavaDoc getMavenRepositoryDirectory()
186     {
187         return new File JavaDoc( getMavenHomeDirectory(), "repository" );
188     }
189
190     private static File JavaDoc getMavenHomeDirectory()
191     {
192         return new File JavaDoc( getMavenHome() );
193     }
194
195     private static String JavaDoc getMavenHome()
196     {
197         try
198         {
199             String JavaDoc local =
200               System.getProperty(
201                 "maven.home.local",
202                 Env.getEnvVariable( "MAVEN_HOME_LOCAL" ) );
203             if( null != local ) return local;
204
205             return System.getProperty( "user.home" ) + File.separator + ".maven";
206
207         }
208         catch( IOException JavaDoc e )
209         {
210             final String JavaDoc error =
211               "Internal error while attempting to access environment.";
212             final String JavaDoc message =
213               ExceptionHelper.packException( error, e, true );
214             throw new RuntimeException JavaDoc( message );
215         }
216     }
217
218     Configuration getConfiguration( File JavaDoc file ) throws Exception JavaDoc
219     {
220         DefaultConfigurationBuilder builder =
221           new DefaultConfigurationBuilder();
222         return builder.buildFromFile( file );
223     }
224 }
225
Popular Tags