KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > client > SingleHostSolverManager


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.client;
6
7 import java.io.BufferedReader JavaDoc;
8 import java.io.InputStreamReader JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import org.omg.CORBA.ORB JavaDoc;
16 import org.omg.CORBA.Object JavaDoc;
17
18 import ve.luz.ica.jackass.solver.ComponentSolver;
19 import ve.luz.ica.jackass.solver.ComponentSolverHelper;
20
21 /**
22  * Provides an implementation of SolverManager suitable for non-clustered
23  * environments.
24  * This implementation only uses the solver specified in the first line of the
25  * file named as specified by the <code>jackass.solvers</code> system property.
26  * @author Guido Urdaneta
27  * @see SolverManager
28  */

29 final class SingleHostSolverManager implements SolverManager
30 {
31     private static final Log LOG = LogFactory.getLog(SingleHostSolverManager.class);
32     private static final String JavaDoc SOLVER_FILE_NAME = "jackass.solvers";
33     private List JavaDoc solvers;
34     private ORB JavaDoc orb;
35
36     /**
37      * Creates a new SingleHostSolverManager.
38      * @param newOrb The ORB to use to convert stringified or corbaloc reference to
39      * usable CORBA object references
40      */

41     public SingleHostSolverManager(ORB JavaDoc newOrb)
42     {
43         this.orb = newOrb;
44         if (LOG.isDebugEnabled()) LOG.debug("Assigned ORB = " + orb);
45         LOG.info("Current dir is " + System.getProperty("user.dir"));
46         try
47         {
48             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
49                     getClass().getClassLoader().getResourceAsStream(SOLVER_FILE_NAME)));
50             if (LOG.isDebugEnabled()) LOG.debug("Opened configuration file " + SOLVER_FILE_NAME);
51             solvers = new ArrayList JavaDoc();
52
53             String JavaDoc line;
54             while ((line = reader.readLine()) != null)
55             {
56                 ComponentSolver aSolver = ComponentSolverHelper.narrow(orb.string_to_object(line));
57                 //improve error handling
58
if (LOG.isDebugEnabled()) LOG.debug("Processed solver " + line + " as " + aSolver);
59                 solvers.add(aSolver);
60             }
61         }
62         catch (Exception JavaDoc e)
63         {
64             //maybe fatal and System.exit
65
if (LOG.isErrorEnabled())
66                 LOG.error("Error creating solvers from configuration file " + SOLVER_FILE_NAME, e);
67             throw new IllegalStateException JavaDoc(e.getMessage());
68         }
69     }
70
71     public Object JavaDoc[] solve(String JavaDoc componentID)
72     {
73         ComponentSolver solver = selectSolver();
74         if (LOG.isDebugEnabled()) LOG.debug("Resolving " + componentID);
75         Object JavaDoc[] ret = solver.solve(componentID);
76         if (LOG.isDebugEnabled()) LOG.debug("Result: " + ret);
77         return ret;
78     }
79
80     /**
81      * Returns a reference to the first solver read in the configuration file.
82      * @return see above.
83      */

84     private ComponentSolver selectSolver()
85     {
86         return (ComponentSolver) solvers.get(0);
87     }
88 }
89
Popular Tags