KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tutorial > Main


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package tutorial;
19
20 import java.io.File JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.repository.Artifact;
24 import org.apache.avalon.repository.provider.Builder;
25 import org.apache.avalon.repository.provider.Factory;
26 import org.apache.avalon.repository.provider.InitialContextFactory;
27 import org.apache.avalon.repository.provider.InitialContext;
28 import org.apache.avalon.repository.main.DefaultInitialContextFactory;
29 import org.apache.avalon.repository.Artifact;
30
31 /**
32  * An example of the embedding of a merlin kernel inside a main
33  * method. The objective of the example is to demonstrate a
34  * simple embedded scenario.
35  */

36 public class Main
37 {
38     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
39     {
40         //
41
// Create the initial context factory. This establishes
42
// the application group from which properties will
43
// be resolved. It also provides operations supporting
44
// customization of the application environment.
45
//
46

47         InitialContextFactory initial =
48           new DefaultInitialContextFactory( "merlin" );
49         File JavaDoc home = initial.getHomeDirectory();
50         initial.setCacheDirectory( new File JavaDoc( home, "system" ) );
51         InitialContext context = initial.createInitialContext();
52
53         //
54
// Using the initial context we can now load any repository
55
// application using an artifact specification. Meta
56
// information associated with the artifact is used to
57
// construct the classloader that the application needs in
58
// order to execute.
59
//
60

61         String JavaDoc spec = "artifact:merlin/merlin-impl#3.3-SNAPSHOT";
62         Artifact artifact = Artifact.createArtifact( spec );
63         Builder builder = context.newBuilder( artifact );
64
65         //
66
// With the classloader established we can go ahead and
67
// and get the application factory. The factory has already
68
// been parameterized with defaults derived from properties
69
// based on the application group. We can provide
70
// overriding values by setting the factory criteria to
71
// application specific values following which we instantiate
72
// the application.
73
//
74

75         Factory factory = builder.getFactory();
76         Map JavaDoc criteria = factory.createDefaultCriteria();
77         factory.create( criteria );
78     }
79 }
80
Popular Tags