KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > assembly > Assembler


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

20
21 import org.apache.beehive.controls.api.bean.ControlImplementation;
22 import org.apache.beehive.controls.api.assembly.ControlAssemblyContext;
23 import org.apache.beehive.controls.api.assembly.ControlAssemblyException;
24 import org.apache.beehive.controls.api.assembly.ControlAssembler;
25 import org.apache.beehive.controls.api.assembly.DefaultControlAssembler;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 /**
33  * Helper class to execute assembly logic.
34  */

35 public class Assembler
36 {
37     /**
38      * Executes basic assembly algorithm. For each control type & impl specified, query each impl for the presence
39      * of an assembler -- for each assembler present, build the specified ControlAssemblyContext implementation,
40      * create an instance of the assembler and execute it.
41      *
42      * @param moduleRoot dir root of the module
43      * @param moduleName name of the module
44      * @param srcOutputRoot dir where assemblers can output source files
45      * @param factoryName name of the ControlAssemblyContext factory to use
46      * @param controlTypeToImpl map of control type name to control impl for all control types to be assembled in this module
47      * @param controlTypeToClients map of control type name to a set of control clients (in this module) that use this type
48      * @param cl classloader used to load factories and assemblers
49      * @throws ControlAssemblyException
50      * @throws IOException
51      */

52     public static void assemble( File JavaDoc moduleRoot,
53                                  String JavaDoc moduleName,
54                                  File JavaDoc srcOutputRoot,
55                                  String JavaDoc factoryName,
56                                  Map JavaDoc<String JavaDoc,String JavaDoc> controlTypeToImpl,
57                                  Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> controlTypeToClients,
58                                  ClassLoader JavaDoc cl )
59         throws ControlAssemblyException, IOException JavaDoc
60     {
61         if ( !moduleRoot.exists() || !srcOutputRoot.exists() )
62             throw new IOException JavaDoc( "Directories " + moduleRoot + " or " + srcOutputRoot + " don't exist!");
63
64         if ( factoryName == null )
65             throw new ControlAssemblyException( "Missing context factory names" );
66
67         if ( cl == null )
68             throw new ControlAssemblyException( "Must specify a classloader" );
69
70         ClassLoader JavaDoc origCL = Thread.currentThread().getContextClassLoader();
71         Thread.currentThread().setContextClassLoader( cl );
72
73         try
74         {
75             // Create the requested ControlAssemblyContext.Factory
76
Class JavaDoc factoryClass = cl.loadClass( factoryName );
77             ControlAssemblyContext.Factory factory = (ControlAssemblyContext.Factory)factoryClass.newInstance();
78
79             // Iterate over control types
80
Set JavaDoc<String JavaDoc> controlTypes = controlTypeToImpl.keySet();
81             for ( String JavaDoc ct : controlTypes )
82             {
83                 // Search for applicable ControlAssemblers as specified on the control impls
84
String JavaDoc cImpl = controlTypeToImpl.get( ct );
85                 Class JavaDoc cImplClass = cl.loadClass( cImpl );
86
87                 ControlImplementation a = (ControlImplementation)cImplClass.getAnnotation(ControlImplementation.class);
88                 if ( a == null )
89                     throw new ControlAssemblyException( "Control implementation class=" + cImpl + " missing ControlImplementation annotation" );
90
91                 // For each non-default ControlAssembler, create one and call it.
92
Class JavaDoc<? extends ControlAssembler> assemblerClass = a.assembler();
93                 if ( !assemblerClass.equals(DefaultControlAssembler.class) )
94                 {
95                     ControlAssembler assembler = assemblerClass.newInstance();
96                     Set JavaDoc<String JavaDoc> clients = controlTypeToClients.get( ct );
97                     ControlAssemblyContext cac = factory.newInstance(
98                         cl.loadClass(ct), null, clients, moduleRoot, moduleName, srcOutputRoot );
99                     assembler.assemble( cac );
100                 }
101             }
102         }
103         catch ( ControlAssemblyException cae )
104         {
105             // just rethrow ControlAssemblyExceptions, which will typically come from user-provided assemblers.
106
throw cae;
107         }
108         catch ( Throwable JavaDoc t )
109         {
110             // Not expecting any throwables other than ControlAssemblyExceptions, so consider them as
111
// unexpected infrastructure issues and wrap them in a CAE.
112
throw new ControlAssemblyException( "Assembly infrastructure exception", t);
113         }
114         finally
115         {
116             Thread.currentThread().setContextClassLoader( origCL );
117         }
118     }
119 }
120
Popular Tags