Skip to content

Commit ed21cff

Browse files
author
anysome
committed
1 parent 8cd1f9e commit ed21cff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+540
-370
lines changed

core/jangod.config.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#timezone = Asia/Shanghai
33
#file.encoding = utf-8
44
#file.root = E:/
5-
#file.loader = net.asfun.jangod.base.FileLoader
6-
#file.cacher = net.asfun.jangod.cache.NoopStorage
5+
#file.locater = net.asfun.jangod.base.FileLoader
6+
file.cache = net.asfun.jangod.cache.NoopStorage
7+
#parser.cache = net.asfun.jangod.cache.NoopStorage
78
#processor.pool = net.asfun.jangod.cache.ConcurrentListPool
89
lib.imports = net.asfun.jangod.lib.filter.ReverseFilter \
910
net.asfun.jangod.lib.filter.RandomFilter \

core/net/asfun/jangod/base/Application.java

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,20 @@
1515
**********************************************************************/
1616
package net.asfun.jangod.base;
1717

18-
import static net.asfun.jangod.util.logging.JangodLogger;
19-
20-
import java.io.IOException;
2118
import java.util.Hashtable;
2219
import java.util.Map;
2320

24-
import net.asfun.jangod.cache.StatelessObjectStorage;
25-
import net.asfun.jangod.cache.SynchronousStorage;
26-
2721
public class Application {
2822

2923
Map<String, Object> globalBindings = new Hashtable<String, Object>(5);
3024
Configuration config;
31-
ResourceLoader loader;
32-
StatelessObjectStorage<String, String> resourceCache;
33-
3425

3526
public Application() {
3627
config = Configuration.getDefault().clone();
37-
init();
3828
}
3929

4030
public Application(String configFile) {
4131
config = ConfigInitializer.getConfig(configFile);
42-
init();
4332
}
4433

4534
public Map<String, Object> getGlobalBindings() {
@@ -50,54 +39,6 @@ public void setGlobalBindings(Map<String, Object> globalBindings) {
5039
this.globalBindings = globalBindings;
5140
}
5241

53-
@SuppressWarnings("unchecked")
54-
private void init() {
55-
//file loader
56-
String loaderClass = config.getProperty("file.loader", "net.asfun.jangod.base.FileLoader");
57-
try {
58-
loader = (ResourceLoader) Class.forName(loaderClass).newInstance();
59-
} catch (Exception e) {
60-
loader = new FileLoader();
61-
JangodLogger.warning("Can't instance file loader(use default) >>> " + loaderClass);
62-
}
63-
loader.setConfiguration(config);
64-
//file cacher
65-
String storeClass = config.getProperty("file.cacher");
66-
if ( storeClass == null ) {
67-
resourceCache = new SynchronousStorage<String,String>();
68-
} else {
69-
try {
70-
resourceCache = (StatelessObjectStorage<String, String>) Class.forName(storeClass).newInstance();
71-
} catch (Exception e) {
72-
resourceCache = new SynchronousStorage<String,String>();
73-
JangodLogger.warning("Can't instance file cacher(use default) >>> " + loaderClass);
74-
}
75-
}
76-
}
77-
78-
public String getResource(String file, String directory) throws IOException {
79-
return getResource(file, config.getEncoding(), directory);
80-
}
81-
82-
public String getResource(String file, String encoding, String directory) throws IOException {
83-
String key;
84-
if ( directory != null ) {
85-
key = directory + file;
86-
} else {
87-
key = file;
88-
}
89-
String value = resourceCache.get(key);
90-
if ( value == null ) {
91-
value = loader.getString(file, encoding, directory);
92-
resourceCache.put(key, value);
93-
}
94-
return value;
95-
}
96-
97-
public String getDirectory(String file) {
98-
return loader.getDirectory(file, config.getWorkspace());
99-
}
100-
10142
public Configuration getConfiguration() {
10243
return config;
10344
}

core/net/asfun/jangod/base/Configuration.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public class Configuration implements Cloneable{
3737
private TimeZone timezone;
3838
private String workspace;
3939
Properties properties = new Properties();
40+
static final Configuration config;
41+
42+
static {
43+
config = ConfigInitializer.getConfig(null);
44+
}
4045

4146
protected Configuration(){};
4247

@@ -82,7 +87,7 @@ public void setTimezone(TimeZone timezone) {
8287
}
8388

8489
public static Configuration getDefault() {
85-
return ConfigInitializer.getConfig(null);
90+
return config;
8691
}
8792

8893
public String getWorkspace() {
@@ -91,8 +96,8 @@ public String getWorkspace() {
9196

9297
public void setWorkspace(String rootPath) {
9398
if ( rootPath == null) return;
94-
if ( ! rootPath.endsWith(File.separator) ) {
95-
workspace = rootPath + File.separator;
99+
if ( rootPath.endsWith(File.separator) ) {
100+
workspace = rootPath.substring(0, rootPath.lastIndexOf(File.separator));
96101
} else {
97102
workspace = rootPath;
98103
}

core/net/asfun/jangod/base/Context.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ public Configuration getConfiguration() {
5555
public void setFile(String file) {
5656
this.file = file;
5757
}
58+
59+
public String getWorkspace() {
60+
if ( file != null ) {
61+
try {
62+
return ResourceManager.getDirectory(file);
63+
} catch ( IOException e) {
64+
return application.config.getWorkspace();
65+
}
66+
} else {
67+
return application.config.getWorkspace();
68+
}
69+
}
5870

5971
public Object getAttribute(String varName, int scope) {
6072
switch ( scope ) {
@@ -128,13 +140,5 @@ public void reset(int scope) {
128140
throw new IllegalArgumentException("Illegal scope value.");
129141
}
130142
}
131-
132-
public String getRelatedResource(String templateFile) throws IOException {
133-
if ( file != null ) {
134-
return application.getResource(templateFile, application.getDirectory(file));
135-
} else {
136-
return application.getResource(templateFile, null);
137-
}
138-
}
139143

140144
}

core/net/asfun/jangod/base/FileLoader.java

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**********************************************************************
2+
Copyright (c) 2010 Asfun Net.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
**********************************************************************/
16+
package net.asfun.jangod.base;
17+
18+
import java.io.BufferedReader;
19+
import java.io.File;
20+
import java.io.FileInputStream;
21+
import java.io.IOException;
22+
import java.io.InputStreamReader;
23+
import java.io.Reader;
24+
25+
public class FileLocater implements ResourceLocater {
26+
27+
@Override
28+
public String getDirectory(String fullName) throws IOException {
29+
File file = new File(fullName);
30+
if ( file.isFile() ) {
31+
return file.getParentFile().getCanonicalPath();
32+
}
33+
return file.getCanonicalPath();
34+
}
35+
36+
@Override
37+
public String getFullName(String relativeName, String relativeDir, String defaultDir) throws IOException {
38+
File file = new File(relativeName);
39+
if (file.exists() && file.isFile()) {
40+
return file.getCanonicalPath();
41+
}
42+
file = new File(relativeDir + File.separator + relativeName);
43+
if (file.exists() && file.isFile()) {
44+
return file.getCanonicalPath();
45+
}
46+
file = new File(defaultDir + File.separator + relativeName);
47+
if (file.exists() && file.isFile()) {
48+
return file.getCanonicalPath();
49+
}
50+
throw new IOException("File not found >>> '" + relativeName + "' in "
51+
+ relativeDir + " and " + defaultDir);
52+
}
53+
54+
@Override
55+
public String getFullName(String relativeName, String defaultDir) throws IOException {
56+
File file = new File(relativeName);
57+
if (file.exists() && file.isFile()) {
58+
return file.getCanonicalPath();
59+
}
60+
file = new File(defaultDir + File.separator + relativeName);
61+
if (file.exists() && file.isFile()) {
62+
return file.getCanonicalPath();
63+
}
64+
throw new IOException("File not found >>> '" + relativeName + "' in " + defaultDir);
65+
}
66+
67+
@Override
68+
public Reader getReader(String fullName, String encoding) throws IOException {
69+
File file = new File(fullName);
70+
if (file.exists() && file.isFile()) {
71+
return new InputStreamReader(new FileInputStream(file), encoding);
72+
}
73+
throw new IOException("File not found >>> " + fullName);
74+
}
75+
76+
@Override
77+
public String getString(String fullName, String encoding) throws IOException {
78+
Reader reader = getReader(fullName, encoding);
79+
BufferedReader br = new BufferedReader(reader);
80+
StringBuffer buff = new StringBuffer();
81+
String line;
82+
try {
83+
while( (line=br.readLine()) != null ) {
84+
buff.append(line);
85+
buff.append(NEW_LINE);
86+
}
87+
} catch (IOException e) {
88+
throw e;
89+
} finally {
90+
reader.close();
91+
}
92+
return buff.toString();
93+
}
94+
95+
}

0 commit comments

Comments
 (0)