KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > dao > file > FileProcessorFactory


1 /*
2  * $Id: FileProcessorFactory.java,v 1.3 2005/06/07 12:32:16 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitriy Belov <bel@jresearch.org>
23  * .
24  * * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on Aug 8, 2004
27  *
28  */

29 package org.jresearch.gossip.dao.file;
30
31 import java.io.IOException JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 import org.jresearch.gossip.exception.SystemException;
38
39 /**
40  * @author Bel
41  *
42  */

43 public class FileProcessorFactory {
44
45     private Map JavaDoc registry = Collections.synchronizedMap(new HashMap JavaDoc());
46
47     private Properties JavaDoc props = new Properties JavaDoc();
48
49     private static FileProcessorFactory instance;
50
51     public final String JavaDoc SUFF_STORENAME = ".storename";
52
53     public final String JavaDoc SUFF_CLASSNAME = ".processor.class";
54
55     private FileProcessorFactory() throws SystemException {
56         try {
57             // load(or fill) properties
58
props.load(this.getClass().getClassLoader().getResourceAsStream(
59                     "org/jresearch/gossip/dao/file/filedb.properties"));
60         } catch (IOException JavaDoc e) {
61             throw new SystemException(e);
62         }
63
64     }
65
66     /**
67      * @return
68      * @throws SystemException
69      */

70     public static synchronized FileProcessorFactory getInstance()
71             throws SystemException {
72         if (null == instance) {
73             instance = new FileProcessorFactory();
74         }
75         return instance;
76     }
77
78     /**
79      * @param name
80      * @return
81      * @throws SystemException
82      */

83     public IFileProcessor getFileProcessor(String JavaDoc name) throws SystemException {
84         IFileProcessor fp = null;
85         if (!registry.containsKey(name)) {
86             loadFileProcessor(name);
87         }
88         fp = (IFileProcessor) registry.get(name);
89         return fp;
90     }
91
92     /**
93      * @param name
94      * @throws SystemException
95      */

96     private void loadFileProcessor(String JavaDoc name) throws SystemException {
97         String JavaDoc className = props.getProperty(name + SUFF_CLASSNAME);
98         String JavaDoc storeName = props.getProperty(name + SUFF_STORENAME);
99         try {
100             IFileProcessor fp = (IFileProcessor) Class.forName(className)
101                     .newInstance();
102             fp.setStoreName(storeName);
103             registry.put(name, fp);
104
105         } catch (InstantiationException JavaDoc e) {
106             throw new SystemException(e);
107         } catch (IllegalAccessException JavaDoc e) {
108             throw new SystemException(e);
109         } catch (ClassNotFoundException JavaDoc e) {
110             throw new SystemException(e);
111         }
112     }
113 }
Popular Tags