KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > core > ResinImport


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.config.core;
30
31 import com.caucho.config.Config;
32 import com.caucho.config.ConfigELContext;
33 import com.caucho.config.ConfigException;
34 import com.caucho.config.SchemaBean;
35 import com.caucho.config.types.FileSetType;
36 import com.caucho.loader.Environment;
37 import com.caucho.log.Log;
38 import com.caucho.util.L10N;
39 import com.caucho.vfs.Depend;
40 import com.caucho.vfs.Path;
41
42 import javax.annotation.PostConstruct;
43 import javax.el.ELContext;
44 import java.util.ArrayList JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Imports values from a separate file.
49  */

50 public class ResinImport extends ResinControl
51 {
52   private static final L10N L = new L10N(ResinImport.class);
53   private static final Logger JavaDoc log = Log.open(ResinImport.class);
54
55   private Path _path;
56   private FileSetType _fileSet;
57   private boolean _isOptional;
58
59   /**
60    * Sets the resin:import path.
61    */

62   public void setPath(Path path)
63   {
64     _path = path;
65   }
66
67   /**
68    * Sets the resin:import fileset.
69    */

70   public void setFileset(FileSetType fileSet)
71   {
72     _fileSet = fileSet;
73   }
74   
75   /**
76    * Sets true if the path is optional.
77    */

78   public void setOptional(boolean optional)
79   {
80     _isOptional = optional;
81   }
82
83   @PostConstruct
84   public void init()
85     throws Exception JavaDoc
86   {
87     if (_path == null) {
88       if (_fileSet == null)
89     throw new ConfigException(L.l("'path' attribute missing from resin:import."));
90     }
91     else if (_path.canRead() && ! _path.isDirectory()) {
92     }
93     else if (_isOptional) {
94       log.finer(L.l("resin:import '{0}' is not readable.", _path));
95
96       Environment.addDependency(new Depend(_path));
97       return;
98     }
99     else {
100       throw new ConfigException(L.l("Required file '{0}' can not be read for resin:import.",
101                     _path.getNativePath()));
102     }
103     
104     Object JavaDoc object = getObject();
105
106     String JavaDoc schema = null;
107     // Use the relax schema for beans with schema.
108
if (object instanceof SchemaBean) {
109       schema = ((SchemaBean) object).getSchema();
110     }
111
112     ArrayList JavaDoc<Path> paths;
113
114     if (_fileSet != null)
115       paths = _fileSet.getPaths();
116     else {
117       paths = new ArrayList JavaDoc<Path>();
118       paths.add(_path);
119     }
120
121     for (int i = 0; i < paths.size(); i++) {
122       Path path = paths.get(i);
123
124       log.config(L.l("resin:import '{0}'", path.getNativePath()));
125
126       Environment.addDependency(new Depend(path));
127
128       Config config = new Config();
129       // server/10hc
130
// config.setResinInclude(true);
131

132       ELContext elContext = Config.getEnvironment();
133       if (elContext instanceof ConfigELContext) {
134     config.setELContext((ConfigELContext) elContext);
135       }
136
137       config.configureBean(object, path, schema);
138     }
139   }
140 }
141
142
Popular Tags