Return-Path: Delivered-To: apmail-avalon-cvs-archive@www.apache.org Received: (qmail 15318 invoked from network); 25 Jan 2004 17:30:27 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 25 Jan 2004 17:30:27 -0000 Received: (qmail 19091 invoked by uid 500); 25 Jan 2004 17:30:19 -0000 Delivered-To: apmail-avalon-cvs-archive@avalon.apache.org Received: (qmail 19015 invoked by uid 500); 25 Jan 2004 17:30:19 -0000 Mailing-List: contact cvs-help@avalon.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon CVS List" Reply-To: "Avalon Developers List" Delivered-To: mailing list cvs@avalon.apache.org Received: (qmail 18988 invoked from network); 25 Jan 2004 17:30:18 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 25 Jan 2004 17:30:18 -0000 Received: (qmail 15257 invoked by uid 1438); 25 Jan 2004 17:30:25 -0000 Date: 25 Jan 2004 17:30:25 -0000 Message-ID: <20040125173025.15256.qmail@minotaur.apache.org> From: mcconnell@apache.org To: avalon-cvs@apache.org Subject: cvs commit: avalon/repository/site/xdocs/start/tutorial index.xml X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N mcconnell 2004/01/25 09:30:25 Modified: repository/site/xdocs navigation.xml repository/site/xdocs/start/tutorial index.xml Log: Update the repository tutorial with a more complete introduction to embedding. Revision Changes Path 1.3 +6 -1 avalon/repository/site/xdocs/navigation.xml Index: navigation.xml =================================================================== RCS file: /home/cvs/avalon/repository/site/xdocs/navigation.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- navigation.xml 23 Dec 2003 17:31:25 -0000 1.2 +++ navigation.xml 25 Jan 2004 17:30:25 -0000 1.3 @@ -63,7 +63,12 @@ - + + + + + + 1.3 +236 -88 avalon/repository/site/xdocs/start/tutorial/index.xml Index: index.xml =================================================================== RCS file: /home/cvs/avalon/repository/site/xdocs/start/tutorial/index.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- index.xml 11 Dec 2003 12:24:23 -0000 1.2 +++ index.xml 25 Jan 2004 17:30:25 -0000 1.3 @@ -1,51 +1,20 @@ - @@ -57,57 +26,236 @@ -
+
-

The following example is taken from the the Merlin project. - It demonstrates the creation of an embedded merlin kernel using + It demonstrates the creation of an embedded logging service using the Repository Facility builder, factory and criteria handling patterns.

- -try -{ - // - // declare the facility to load - // - - Artifact artifact = - Artifact.createArtifact( - "merlin", "merlin-impl", "3.2-dev" ); - - // - // create an inital context - // - - InitialContext context = - new DefaultInitialContext(); - - // - // create the factory using the default builder - // - - Builder builder = new DefaultBuilder( context, artifact ); - Factory factory = builder.getFactory(); - - // - // get the default factory parameters and apply these - // as the factory argument to construct the new facility - // - - Map criteria = factory.createDefaultCriteria(); - applyLocalProperties( criteria ); - Object facility = factory.create( criteria ); -} -catch( Throwable e ) + + +

+ The following code fragment defines a cache directory for the + repository system to use when loading resources needed in your + embedded application - and provides the directory as an argument + when creating a new InitialContext object. The InitialContext is + your hook into the repository system and the embedding machinery. +

+ +File cache = new File( "my-cache" ); +InitialContext context = new DefaultInitialContext( cache ); + +
+ + + +String spec = + "artifact:avalon-logging/avalon-logkit?version=1.0-SNAPSHOT" +Artifact artifact = + Artifact.createArtifact( spec ); + +

+ An artifact is a logical reference to a jar file (or other type + of resource) that the repository can access. The avalon-repository + system uses artifact references as the key to locating meta data + about embedded classloaders. The classloader meta data is + maintained as a properties file with the .meta extension. For + example the above artifact meta address translates to: +

+ +[host]/avalon-logging/jars/avalon-logkit-impl-1.0-SNAPSHOT.jar.meta + +

+ The content of the meta file is automatically generated using the + avalon-plugin avalon:artifact goal. Two examples of factory meta + are provides, one concerning the logkit + factory and a second describing the merlin + factory. +

+

+ The contents of the meta file includes: +

+
    +
  • an ordered list of jar files that are required to + construct a classloader for the embedded application
  • + +
  • the name of a factory class to be used as the embedded + instance creator
  • +
+
+ + +

+ Using the initial context and the artifact you now have everything + you need to create you embedded instance. +

+ +Builder builder = m_context.newBuilder( artifact ); +Factory factory = builder.getFactory(); + +

+ Behind the scenes the avalon-repository system has gone off, pulled + down the meta data, downloaded and cached all of the classloader + entries, constructed a new classloader, and instantiated the + factory. +

+
+ + +

+ The factory object is the central entry point of the embedded + application - it is responsible for instantiation of the + embedded instance based on a supplied criteria. The initial + criteria (the defaults) are established by the factory in + response to the following operation: +

+ +Map criteria = factory.createDefaultCriteria(); + +

+ Based on the documentation about the facility you are embedding + you can update the criteria using application specific keys. All + of the Merlin related criteria instances use the avalon-util + Criteria as the abstract base class for the map implementations. + This provides support for key and type validation under the put + operations and type coercion on get operations. +

+

+ For example: +

+ +String key = "avalon.logging.configuration"; +File file = new File( "logging.xml" ); +criteria.put( key, file ); + +

+ Parameterization of the criteria is typically different for + each embedding scenario. A CLI handler will for example adapt + to the command line operations and set criteria values accordingly. + A web application may set the criteria based on parameters declared + in a web.xml file. Typically the embedding class acts as the + adapter between the embedded context and the factory. +

+
+ + +

+ Creation of the embedded instance is now a simple one line + operation: +

+ +Object object = factory.create( criteria ); + +

+ The object that is created is the embedded application. In this + example its a logging manager that uses the logkit implementation. + However, it could have been the Merlin kernel. The only difference + between the Merlin scenario and the logging manager scenario is + the initial artifact and the actions taken to parameterize the + criteria. +

+
+ + + +// +// create the initial referecne +// + +File cache = new File( "my-cache" ); +InitialContext context = new DefaultInitialContext( cache ); + +// +// define the artifact +// + +String spec = + "artifact:avalon-logging/avalon-logkit?version=1.0-SNAPSHOT" +Artifact artifact = + Artifact.createArtifact( spec ); + +// +// create the builder, get the factory and the initial criteria +// + +Builder builder = m_context.newBuilder( artifact ); +Factory factory = builder.getFactory(); +Map criteria = factory.createDefaultCriteria(); + +// +// customize the crieria +// + +String key = "avalon.logging.configuration"; +File file = new File( "logging.xml" ); +criteria.put( key, file ); + +// +// create the embedded instance +// + +LoggingManager manager = + (LoggingManager) factory.create( criteria ); + + + + + +

+The avalon-repository API provides special support for initial context propergation to the factory that is being created. The allows the factory to embed additional applications within itself using the classloader it is constructed under, together with the original initial context. +

+

+The following code snipet demonstrates a factory that caches references to the classloader and initial context so that it can use these is subsequent embedding of services during its own create method implementation. +

+ +public class WidgetFactory implements Factory { - final String error = ExceptionHelper.packException( e, true ); - throw new Exception( error ); + private final InitialContext m_context; + private final ClassLoader m_classloader; + + public WidgetFactory( + InitialContext context, ClassLoader classloader ) + { + m_context = context; + m_classloader = classloader; + } + + public Map getDefaultCriteria() + { + return new WidgetContext(); + } + + public Object create() + { + return create( getDefaultCriteria() ); + } + + public Object create( Map criteria ) + { + // + // embed something into this factory using + // the classloader that established this factory + // as the parent classloader of the thing we are + // embedding + // + + Artifact artifact = + (Artifact) criteria.get( "gizmo.artifact" ); + Builder builder = m_context.newBuilder( m_classloader, gizmo ); + Gizmo gizmo = builder.getFactory().create(); + + // + // do other stuff + // + + return new DefaultWidget( gizmo ); + } } - +
-
+
--------------------------------------------------------------------- To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org For additional commands, e-mail: cvs-help@avalon.apache.org