1
17 package org.apache.catalina.startup;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.MalformedURLException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.nio.file.InvalidPathException;
28
29 import org.apache.tomcat.util.file.ConfigurationSource;
30 import org.apache.tomcat.util.res.StringManager;
31
32 public class CatalinaBaseConfigurationSource implements ConfigurationSource {
33
34 protected static final StringManager sm = StringManager.getManager(Constants.Package);
35
36 private final String serverXmlPath;
37 private final File catalinaBaseFile;
38 private final URI catalinaBaseUri;
39
40 public CatalinaBaseConfigurationSource(File catalinaBaseFile, String serverXmlPath) {
41 this.catalinaBaseFile = catalinaBaseFile;
42 catalinaBaseUri = catalinaBaseFile.toURI();
43 this.serverXmlPath = serverXmlPath;
44 }
45
46 @Override
47 public Resource getServerXml() throws IOException {
48 IOException ioe = null;
49 Resource result = null;
50 try {
51 if (serverXmlPath == null || serverXmlPath.equals(Catalina.SERVER_XML)) {
52 result = ConfigurationSource.super.getServerXml();
53 } else {
54 result = getResource(serverXmlPath);
55 }
56 } catch (IOException e) {
57 ioe = e;
58 }
59 if (result == null) {
60
61 InputStream stream = getClass().getClassLoader().getResourceAsStream("server-embed.xml");
62 if (stream != null) {
63 try {
64 result = new Resource(stream, getClass().getClassLoader().getResource("server-embed.xml").toURI());
65 } catch (URISyntaxException e) {
66 stream.close();
67 }
68 }
69 }
70
71 if (result == null && ioe != null) {
72 throw ioe;
73 } else {
74 return result;
75 }
76 }
77
78 @Override
79 public Resource getResource(String name) throws IOException {
80
81
82
83 File f = new File(name);
84 if (!f.isAbsolute()) {
85 f = new File(catalinaBaseFile, name);
86 }
87 if (f.isFile()) {
88 return new Resource(new FileInputStream(f), f.toURI());
89 }
90
91
92 InputStream stream = getClass().getClassLoader().getResourceAsStream(name);
93 if (stream != null) {
94 try {
95 return new Resource(stream, getClass().getClassLoader().getResource(name).toURI());
96 } catch (InvalidPathException e) {
97
98 stream.close();
99 } catch (URISyntaxException e) {
100 stream.close();
101 throw new IOException(sm.getString("catalinaConfigurationSource.cannotObtainURL", name), e);
102 }
103 }
104
105
106 URI uri = null;
107 try {
108 uri = getURI(name);
109 } catch (IllegalArgumentException e) {
110 throw new IOException(sm.getString("catalinaConfigurationSource.cannotObtainURL", name), e);
111 }
112
113
114 try {
115 URL url = uri.toURL();
116 return new Resource(url.openConnection().getInputStream(), uri);
117 } catch (MalformedURLException e) {
118 throw new IOException(sm.getString("catalinaConfigurationSource.cannotObtainURL", name), e);
119 }
120 }
121
122 @Override
123 public URI getURI(String name) {
124 File f = new File(name);
125 if (!f.isAbsolute()) {
126 f = new File(catalinaBaseFile, name);
127 }
128 if (f.isFile()) {
129 return f.toURI();
130 }
131
132
133 try {
134 URL resource = getClass().getClassLoader().getResource(name);
135 if (resource != null) {
136 return resource.toURI();
137 }
138 } catch (Exception e) {
139
140 }
141
142
143
144
145 URI uri;
146 if (catalinaBaseUri != null) {
147 uri = catalinaBaseUri.resolve(name);
148 } else {
149 uri = URI.create(name);
150 }
151 return uri;
152 }
153
154 }
155