001 /*
002  * 11/06/2002 - 11:53:11
003  *
004  * $RCSfile: RepositoryFactory.java,v $ - JDBF Object Relational mapping system
005  * Copyright (C) 2002 JDBF Development Team
006  
007  * http://jdbf.sourceforge.net
008  *
009  * This program is free software; you can redistribute it and/or
010  * modify it under the terms of the GNU Lesser General Public License
011  * as published by the Free Software Foundation; either version 2
012  * of the License, or (at your option) any later version.
013  *
014  * This program is distributed in the hope that it will be useful,
015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017  * GNU Lesser General Public License for more details.
018  *
019  * You should have received a copy of the GNU Lesser General Public License
020  * along with this program; if not, write to the Free Software
021  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022  */
023  
024  /*
025   * $Id: RepositoryFactory.java,v 1.7 2004/05/20 22:41:22 gmartone Exp $
026   */
027 package org.jdbf.engine.repository;
028 import java.util.Collections;
029 import java.util.Map;
030 import java.util.HashMap;
031 import java.util.logging.Logger;
032 import java.util.logging.Level;
033 import org.jdbf.castor.Messages;
034 import org.jdbf.engine.mapping.MappingException;
035 /**
036  <code>RepositoryFactory</code> is a factory of Repository.
037  * Collection of Repository are loaded on startup and are putted 
038  * in this class so they are available.
039  *
040  @author Giovanni Martone
041  @version $Revision: 1.7 $
042  * last changed by $Author: gmartone $
043  *
044  */
045 public class RepositoryFactory{
046   /** Contains Repository */
047   private static Map repositories = Collections.synchronizedMap(new HashMap());
048   
049   /** Logger object */
050     private static Logger logger = Logger.getLogger("org.jdbf.engine.repository");
051   
052   /**
053    * Creates a empty object
054    */
055   public RepositoryFactory(){}
056   /**
057    * Creates a object.
058    *
059    @param map that contains the Repository object
060    */
061   public RepositoryFactory(Map map){
062     repositories = map;
063   }
064   /**
065    * Add Repository with the specific key.
066    *
067    @param key of Repository, the key is the name of RepositoryView
068    @param repository Repository object
069    @throws MappingException if a repository with name specified in key 
070    *        already exits
071    *
072    */
073   public void addRepository(String key, Repository repository)
074     throws MappingException{
075     
076     logger.log(Level.INFO,Messages.format("RepositoryFactory.addRepository", key));
077     if(repositories.containsKey(key))
078         throw new MappingException("mapping.duplicateRepositoryViewName",
079                              key
080                             );
081     else
082         repositories.put(key,repository);
083   }
084   
085   /**
086    * Return the Repository with the name specified in key paramater
087    *
088    @param key name of repository
089    @return Repository
090    *
091    */
092   public Repository getRepository(String key){
093       logger.log(Level.INFO,Messages.format("RepositoryFactory.getRepository", key));
094       return (Repository)repositories.get(key);
095   }
096   /**
097    * Return a Map of Repository
098    *
099    @return Map
100    */
101   public Map getRepositories(){
102       return repositories;
103   }
104   /**
105    * Return a list of ItemDescriptor of a specific repository specified 
106    * in repository name
107    *
108    @param repositoryName
109    @return list 
110    *
111    */
112    public static java.util.List getItemDescriptors(String repositoryName){
113        logger.log(Level.INFO,Messages.format("RepositoryFactory.getItemDescriptors", repositoryName));
114        RepositoryView view = (RepositoryView)repositories.get(repositoryName);
115        return view.getBeanDescriptor().getItemDescriptors();
116    }
117    /**
118     * Return Repository given its name
119     @param name
120     @return Repository
121     */
122    public static Repository getRepositoryFromName(String name){
123        logger.log(Level.INFO,Messages.format("RepositoryFactory.getRepository", name));
124       return (RepositoryViewrepositories.get(name);
125    }
126 }
127 /*
128   $Log $
129 */