1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.tomcat.util.file;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.MalformedURLException;
25 import java.net.URI;
26 import java.net.URL;
27
28 /**
29 * Abstracts configuration file storage. Allows Tomcat embedding using the regular
30 * configuration style.
31 * This abstraction aims to be very simple and does not cover resource listing,
32 * which is usually used for dynamic deployments that are usually not used when
33 * embedding, as well as resource writing.
34 */
35 public interface ConfigurationSource {
36
37 public static final ConfigurationSource DEFAULT = new ConfigurationSource() {
38 protected final File userDir = new File(System.getProperty("user.dir"));
39 protected final URI userDirUri = userDir.toURI();
40 @Override
41 public Resource getResource(String name) throws IOException {
42 File f = new File(name);
43 if (!f.isAbsolute()) {
44 f = new File(userDir, name);
45 }
46 if (f.isFile()) {
47 return new Resource(new FileInputStream(f), f.toURI());
48 }
49 URI uri = null;
50 try {
51 uri = getURI(name);
52 } catch (IllegalArgumentException e) {
53 throw new FileNotFoundException(name);
54 }
55 try {
56 URL url = uri.toURL();
57 return new Resource(url.openConnection().getInputStream(), uri);
58 } catch (MalformedURLException e) {
59 throw new FileNotFoundException(name);
60 }
61 }
62 @Override
63 public URI getURI(String name) {
64 File f = new File(name);
65 if (!f.isAbsolute()) {
66 f = new File(userDir, name);
67 }
68 if (f.isFile()) {
69 return f.toURI();
70 }
71 return userDirUri.resolve(name);
72 }
73 };
74
75 /**
76 * Represents a resource: a stream to the resource associated with
77 * its URI.
78 */
79 public class Resource implements AutoCloseable {
80 private final InputStream inputStream;
81 private final URI uri;
82 public Resource(InputStream inputStream, URI uri) {
83 this.inputStream = inputStream;
84 this.uri = uri;
85 }
86 public InputStream getInputStream() {
87 return inputStream;
88 }
89 public URI getURI() {
90 return uri;
91 }
92 public long getLastModified()
93 throws MalformedURLException, IOException {
94 return uri.toURL().openConnection().getLastModified();
95 }
96 @Override
97 public void close() throws IOException {
98 if (inputStream != null) {
99 inputStream.close();
100 }
101 }
102 }
103
104 /**
105 * Returns the contents of the main conf/server.xml file.
106 * @return the server.xml as an InputStream
107 * @throws IOException if an error occurs or if the resource does not exist
108 */
109 public default Resource getServerXml()
110 throws IOException {
111 return getConfResource("server.xml");
112 }
113
114 /**
115 * Returns the contents of the shared conf/web.xml file. This usually
116 * contains the declaration of the default and JSP servlets.
117 * @return the web.xml as an InputStream
118 * @throws IOException if an error occurs or if the resource does not exist
119 */
120 public default Resource getSharedWebXml()
121 throws IOException {
122 return getConfResource("web.xml");
123 }
124
125 /**
126 * Get a resource, based on the conf path.
127 * @param name The resource name
128 * @return the resource as an InputStream
129 * @throws IOException if an error occurs or if the resource does not exist
130 */
131 public default Resource getConfResource(String name)
132 throws IOException {
133 String fullName = "conf/" + name;
134 return getResource(fullName);
135 }
136
137 /**
138 * Get a resource, not based on the conf path.
139 * @param name The resource name
140 * @return the resource
141 * @throws IOException if an error occurs or if the resource does not exist
142 */
143 public Resource getResource(String name)
144 throws IOException;
145
146 /**
147 * Get a URI to the given resource. Unlike getResource, this will also
148 * return URIs to locations where no resource exists.
149 * @param name The resource name
150 * @return a URI representing the resource location
151 */
152 public URI getURI(String name);
153
154 }
155