001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.core;
020    
021    import gnu.regexp.RE;
022    import java.util.Collection;
023    import java.util.Vector;
024    import org.apache.log4j.Logger;
025    import org.objectweb.jac.util.*;
026    
027    /**
028     * Provides a naming repository within a running JAC system.
029     *
030     * <p>All the JAC objects are seamlessly registered by
031     * <code>NamingAC</code> when they are instantiated.
032     *
033     * @see org.objectweb.jac.aspects.naming.NamingAC */
034    
035    public class NameRepository extends WeakRepository {
036        static Logger logger = Logger.getLogger("repository");
037    
038        /**
039         * Get the sole instance of name repository.
040         * 
041         * @return the name repository */
042       
043        public static Repository get() {
044            if (nameRepository == null) 
045                return new NameRepository();
046            return nameRepository;
047        }
048       
049        /**
050         * Store the sole instance of name repository. */
051        protected static NameRepository nameRepository = null;
052    
053        /**
054         * The default constructor will set the nameRepository field to the
055         * right value. */
056    
057        public NameRepository() {
058            nameRepository = this;
059            register("JAC_name_repository", this);
060            // HACK!
061            /*
062              if (Wrapping.acm != null ) {
063              register ( "JAC_ac_manager", Wrapping.acm );
064              } else {
065              register ( "JAC_ac_manager", ACManager.get() );
066              }
067            */
068            register("JAC_ac_manager",ACManager.get());
069            register("JAC_application_repository", ApplicationRepository.get());
070        }
071    
072        public Object getObject(String logicalName) {
073            if (logicalName == null) 
074                return null;
075            Object ret = objects.get(logicalName);
076            if (ret == null) {
077                ((ACManager)ACManager.get()).whenObjectMiss(logicalName);
078                ret = Collaboration.get().getAttribute(BaseProgramListener.FOUND_OBJECT);
079            }
080            logger.debug("getObject("+logicalName+") -> "+
081                         (ret==null?"null":ret.getClass().getName()));
082            return ret;
083        }
084    
085        /**
086         * Gets the set of JAC objects whose names match an expression.
087         *
088         * @param expr a regular expression
089         * @return the objects set 
090         */
091        public static Collection getObjects(String expr) {
092            NameRepository nr = (NameRepository)get();
093            String[] names = nr.getNames();
094            Collection res = new Vector();
095            RE re;
096            try {
097                re = new RE(expr);
098            } catch (Exception e) {
099                logger.error("Bad regular expression '"+expr+"'",e);
100                return null;
101            }
102            for (int i=0; i<names.length; i++) {
103                String 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 getObjects(String[] exprs) {
115            return getObjects(Strings.join(exprs,"|"));
116        }
117    
118        public static Collection getObjects(Collection exprs) {
119            return getObjects(Strings.join(exprs,"|"));
120        }
121    }