001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source 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, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.config.core;
031:
032: import com.caucho.config.Config;
033: import com.caucho.config.ConfigException;
034: import com.caucho.config.SchemaBean;
035: import com.caucho.config.types.FileSetType;
036: import com.caucho.loader.Environment;
037: import com.caucho.log.Log;
038: import com.caucho.util.L10N;
039: import com.caucho.vfs.Depend;
040: import com.caucho.vfs.Path;
041:
042: import javax.annotation.PostConstruct;
043: import java.util.ArrayList;
044: import java.util.logging.Logger;
045:
046: /**
047: * Imports values from a separate file.
048: */
049: public class ResinImport extends ResinControl {
050: private static final L10N L = new L10N(ResinImport.class);
051: private static final Logger log = Log.open(ResinImport.class);
052:
053: private Path _path;
054: private FileSetType _fileSet;
055: private boolean _isOptional;
056:
057: /**
058: * Sets the resin:import path.
059: */
060: public void setPath(Path path) {
061: if (path == null)
062: throw new NullPointerException(L
063: .l("'path' may not be null for resin:import"));
064:
065: _path = path;
066: }
067:
068: /**
069: * Sets the resin:import fileset.
070: */
071: public void setFileset(FileSetType fileSet) {
072: _fileSet = fileSet;
073: }
074:
075: /**
076: * Sets true if the path is optional.
077: */
078: public void setOptional(boolean optional) {
079: _isOptional = optional;
080: }
081:
082: @PostConstruct
083: public void init() throws Exception {
084: if (_path == null) {
085: if (_fileSet == null)
086: throw new ConfigException(
087: L
088: .l("'path' attribute missing from resin:import."));
089: } else if (_path.canRead() && !_path.isDirectory()) {
090: } else if (_isOptional) {
091: log
092: .finer(L.l("resin:import '{0}' is not readable.",
093: _path));
094:
095: Environment.addDependency(new Depend(_path));
096: return;
097: } else {
098: throw new ConfigException(
099: L
100: .l(
101: "Required file '{0}' can not be read for resin:import.",
102: _path.getNativePath()));
103: }
104:
105: Object object = getObject();
106:
107: String schema = null;
108: // Use the relax schema for beans with schema.
109: if (object instanceof SchemaBean) {
110: schema = ((SchemaBean) object).getSchema();
111: }
112:
113: ArrayList<Path> paths;
114:
115: if (_fileSet != null)
116: paths = _fileSet.getPaths();
117: else {
118: paths = new ArrayList<Path>();
119: paths.add(_path);
120: }
121:
122: for (int i = 0; i < paths.size(); i++) {
123: Path path = paths.get(i);
124:
125: log.config(L.l("resin:import '{0}'", path.getNativePath()));
126:
127: Environment.addDependency(new Depend(path));
128:
129: Config config = new Config();
130: // server/10hc
131: // config.setResinInclude(true);
132:
133: config.configureBean(object, path, schema);
134: }
135: }
136: }
|