KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > NameRepository


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21 import gnu.regexp.RE;
22 import java.util.Collection JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.util.*;
26
27 /**
28  * Provides a naming repository within a running JAC system.
29  *
30  * <p>All the JAC objects are seamlessly registered by
31  * <code>NamingAC</code> when they are instantiated.
32  *
33  * @see org.objectweb.jac.aspects.naming.NamingAC */

34
35 public class NameRepository extends WeakRepository {
36     static Logger logger = Logger.getLogger("repository");
37
38     /**
39      * Get the sole instance of name repository.
40      *
41      * @return the name repository */

42    
43     public static Repository get() {
44         if (nameRepository == null)
45             return new NameRepository();
46         return nameRepository;
47     }
48    
49     /**
50      * Store the sole instance of name repository. */

51     protected static NameRepository nameRepository = null;
52
53     /**
54      * The default constructor will set the nameRepository field to the
55      * right value. */

56
57     public NameRepository() {
58         nameRepository = this;
59         register("JAC_name_repository", this);
60         // HACK!
61
/*
62           if (Wrapping.acm != null ) {
63           register ( "JAC_ac_manager", Wrapping.acm );
64           } else {
65           register ( "JAC_ac_manager", ACManager.get() );
66           }
67         */

68         register("JAC_ac_manager",ACManager.get());
69         register("JAC_application_repository", ApplicationRepository.get());
70     }
71
72     public Object JavaDoc getObject(String JavaDoc logicalName) {
73         if (logicalName == null)
74             return null;
75         Object JavaDoc ret = objects.get(logicalName);
76         if (ret == null) {
77             ((ACManager)ACManager.get()).whenObjectMiss(logicalName);
78             ret = Collaboration.get().getAttribute(BaseProgramListener.FOUND_OBJECT);
79         }
80         logger.debug("getObject("+logicalName+") -> "+
81                      (ret==null?"null":ret.getClass().getName()));
82         return ret;
83     }
84
85     /**
86      * Gets the set of JAC objects whose names match an expression.
87      *
88      * @param expr a regular expression
89      * @return the objects set
90      */

91     public static Collection JavaDoc getObjects(String JavaDoc expr) {
92         NameRepository nr = (NameRepository)get();
93         String JavaDoc[] names = nr.getNames();
94         Collection JavaDoc res = new Vector JavaDoc();
95         RE re;
96         try {
97             re = new RE(expr);
98         } catch (Exception JavaDoc e) {
99             logger.error("Bad regular expression '"+expr+"'",e);
100             return null;
101         }
102         for (int i=0; i<names.length; i++) {
103             String JavaDoc name = names[i];
104             if (name==null) continue;
105             if (re.isMatch(name)) {
106                 res.add(nr.getObject(name));
107             }
108         }
109         logger.debug("getObjects("+expr+") -> "+res);
110         return res;
111     }
112
113
114     public static Collection JavaDoc getObjects(String JavaDoc[] exprs) {
115         return getObjects(Strings.join(exprs,"|"));
116     }
117
118     public static Collection JavaDoc getObjects(Collection JavaDoc exprs) {
119         return getObjects(Strings.join(exprs,"|"));
120     }
121 }
122
Popular Tags