001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment.commandline;
018:
019: import org.apache.commons.collections.iterators.IteratorEnumeration;
020: import org.apache.avalon.framework.logger.AbstractLogEnabled;
021: import org.apache.cocoon.environment.Context;
022:
023: import java.io.File;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.Enumeration;
027: import java.util.Map;
028: import java.util.HashMap;
029: import java.io.InputStream;
030:
031: /**
032: *
033: * Implements the {@link org.apache.cocoon.environment.Context} interface
034: * @author ?
035: * @version CVS $Id: CommandLineContext.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037:
038: public class CommandLineContext extends AbstractLogEnabled implements
039: Context {
040:
041: /** The context directory path*/
042: private String contextDir;
043:
044: /** The context attributes */
045: private Map attributes;
046:
047: /**
048: * Constructs a CommandlineContext object from a ServletContext object
049: */
050: public CommandLineContext(String contextDir) {
051: String contextDirPath = new File(contextDir).getAbsolutePath();
052: // store contextDirPath as is don't remove trailing /.
053: this .contextDir = contextDirPath;
054: this .attributes = new HashMap();
055: }
056:
057: public Object getAttribute(String name) {
058: if (getLogger().isDebugEnabled()) {
059: getLogger().debug(
060: "CommandlineContext: getAttribute=" + name);
061: }
062: return this .attributes.get(name);
063: }
064:
065: public void setAttribute(String name, Object value) {
066: if (getLogger().isDebugEnabled()) {
067: getLogger().debug(
068: "CommandlineContext: setAttribute=" + name);
069: }
070: this .attributes.put(name, value);
071: }
072:
073: public void removeAttribute(String name) {
074: if (getLogger().isDebugEnabled()) {
075: getLogger().debug(
076: "CommandlineContext: removeAttribute=" + name);
077: }
078: this .attributes.remove(name);
079: }
080:
081: public Enumeration getAttributeNames() {
082: if (getLogger().isDebugEnabled()) {
083: getLogger().debug("CommandlineContext: getAttributeNames");
084: }
085: return new IteratorEnumeration(this .attributes.keySet()
086: .iterator());
087: }
088:
089: public URL getResource(String path) throws MalformedURLException {
090: if (getLogger().isDebugEnabled()) {
091: getLogger()
092: .debug("CommandlineContext: getResource=" + path);
093: }
094: // rely on File to build correct File and URL
095: File f = new File(contextDir, path);
096: if (!f.exists())
097: return null;
098: return f.toURL();
099: }
100:
101: public String getRealPath(String path) {
102: if (getLogger().isDebugEnabled()) {
103: getLogger()
104: .debug("CommandlineContext: getRealPath=" + path);
105: }
106: // rely on File to build correct File and URL
107: File f = new File(this .contextDir, path);
108: return f.getAbsolutePath();
109: }
110:
111: public String getMimeType(String file) {
112: if (getLogger().isDebugEnabled()) {
113: getLogger()
114: .debug("CommandlineContext: getMimeType=" + file);
115: }
116: //return servletContext.getMimeType(file);
117: return null;
118: }
119:
120: public String getInitParameter(String name) {
121: getLogger().debug(
122: "CommandlineContext: getInitParameter=" + name);
123: return null;
124: }
125:
126: public InputStream getResourceAsStream(String path) {
127: getLogger().debug(
128: "CommandlineContext: getResourceAsStream " + path);
129: return null;
130: }
131: }
|