001    /*
002     * mesopotamia @mesopotamia.version@
003     * Multilingual parser and repository. 
004     * Copyright (C) 2005  Hammurapi Group
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with this library; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019     *
020     * URL: http://http://www.hammurapi.biz
021     * e-Mail: support@hammurapi.biz
022     */
023    package org.mesopotamia;
024    
025    import java.sql.SQLException;
026    import java.util.Timer;
027    
028    import javax.sql.DataSource;
029    
030    import org.w3c.dom.Element;
031    import org.w3c.dom.Node;
032    
033    import biz.hammurapi.config.Component;
034    import biz.hammurapi.config.ConfigurationException;
035    import biz.hammurapi.config.Context;
036    import biz.hammurapi.config.DomConfigurable;
037    import biz.hammurapi.config.Wrapper;
038    import biz.hammurapi.persistence.StringStorage;
039    import biz.hammurapi.sql.SQLProcessor;
040    import biz.hammurapi.util.Worker;
041    
042    
043    public class RepositoryFactoryComponent implements Component, Wrapper, DomConfigurable {
044    
045            private RepositoryFactory master;
046            private Object owner;
047            private String dataSourceName="/datasource";
048            private String timerName="/timer";
049            private String stringStorageName="/string-storage";
050            private String workerName;
051    
052            public RepositoryFactoryComponent() {
053                    super();
054            }
055    
056            public void start() throws ConfigurationException {
057                    try {
058                            Context context = (Context) owner;
059                            DataSource dataSource = (DataSource) context.get(dataSourceName);
060                            master=new RepositoryFactory(
061                                            new SQLProcessor(dataSource, null), 
062                                            workerName==null ? null : (Worker) context.get(workerName), 
063                                            (Timer) context.get(timerName),
064                                            (StringStorage) context.get(stringStorageName),
065                                            getClass().getClassLoader()); 
066                    } catch (SQLException e) {
067                            throw new ConfigurationException("Cannot create repository factory: "+e,e);
068                    }
069            }
070    
071            public void stop() throws ConfigurationException {
072                    // Nothing to do
073            }
074    
075            public void setOwner(Object owner) {
076                    this.owner=owner;
077            }
078    
079            public Object getMaster() {
080                    return master;
081            }
082    
083            public void configure(Node configNode, Context context, ClassLoader classLoader) throws ConfigurationException {
084                    Element ce=(Element) configNode;
085                    if (ce.hasAttribute("data-source")) {
086                            dataSourceName=ce.getAttribute("data-source");                  
087                    }
088                    
089                    if (ce.hasAttribute("worker")) {
090                            workerName=ce.getAttribute("worker");
091                    }
092            }
093    
094    }