KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > dictionary > DictionaryBootstrap


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.dictionary;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.alfresco.i18n.I18NUtil;
24 import org.alfresco.service.cmr.dictionary.DictionaryException;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28
29 /**
30  * Bootstrap Dictionary DAO with pre-defined models
31  *
32  * @author David Caruana
33  *
34  */

35 public class DictionaryBootstrap
36 {
37     // The list of models to bootstrap with
38
private List JavaDoc<String JavaDoc> models = new ArrayList JavaDoc<String JavaDoc>();
39
40     // The list of model resource bundles to bootstrap with
41
private List JavaDoc<String JavaDoc> resourceBundles = new ArrayList JavaDoc<String JavaDoc>();
42
43     // Dictionary DAO
44
private DictionaryDAO dictionaryDAO = null;
45
46     // Logger
47
private static Log logger = LogFactory.getLog(DictionaryDAO.class);
48     
49     
50     /**
51      * Sets the Dictionary DAO
52      *
53      * @param dictionaryDAO
54      */

55     public void setDictionaryDAO(DictionaryDAO dictionaryDAO)
56     {
57         this.dictionaryDAO = dictionaryDAO;
58     }
59     
60     /**
61      * Sets the initial list of models to bootstrap with
62      *
63      * @param modelResources the model names
64      */

65     public void setModels(List JavaDoc<String JavaDoc> modelResources)
66     {
67         this.models = modelResources;
68     }
69     
70     /**
71      * Sets the initial list of models to bootstrap with
72      *
73      * @param modelResources the model names
74      */

75     public void setLabels(List JavaDoc<String JavaDoc> labels)
76     {
77         this.resourceBundles = labels;
78     }
79     
80     /**
81      * Bootstrap the Dictionary
82      */

83     public void bootstrap()
84     {
85         // register models
86
for (String JavaDoc bootstrapModel : models)
87         {
88             InputStream JavaDoc modelStream = getClass().getClassLoader().getResourceAsStream(bootstrapModel);
89             if (modelStream == null)
90             {
91                 throw new DictionaryException("Could not find bootstrap model " + bootstrapModel);
92             }
93             try
94             {
95                 if (logger.isInfoEnabled())
96                     logger.info("Loading model from " + bootstrapModel);
97                 
98                 M2Model model = M2Model.createModel(modelStream);
99                 dictionaryDAO.putModel(model);
100             }
101             catch(DictionaryException e)
102             {
103                 throw new DictionaryException("Could not import bootstrap model " + bootstrapModel, e);
104             }
105         }
106         
107         // register models
108
for (String JavaDoc resourceBundle : resourceBundles)
109         {
110             I18NUtil.registerResourceBundle(resourceBundle);
111         }
112     }
113
114 }
115
Popular Tags