Replace the use of absolute path with relative path when using a file in the webapp.

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1132415 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
fmoga 2011-06-05 14:19:36 +00:00
parent dc3bc3775a
commit 2f683d4b8b
3 changed files with 38 additions and 37 deletions
sca-java-2.x/contrib/samples/learning-more/binding-comet/single-response-webapp/src/main
java/org/apache/tuscany/sca/sample/comet
resources
webapp/WEB-INF

View file

@ -20,8 +20,8 @@ package org.apache.tuscany.sca.sample.comet;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
@ -39,44 +39,45 @@ import org.oasisopen.sca.annotation.Service;
@Service(CountryRepository.class)
public class CountryRepositoryImpl implements CountryRepository {
@Context
protected ComponentContext context;
@Context
protected ComponentContext context;
private String fileName;
private List<Country> countries;
private String fileName;
private List<Country> countries;
@Constructor
public CountryRepositoryImpl(@Property(name = "fileName") String fileName) {
this.fileName = fileName;
countries = new ArrayList<Country>();
}
@Constructor
public CountryRepositoryImpl(@Property(name = "fileName") String fileName) {
this.fileName = fileName;
countries = new ArrayList<Country>();
}
@Init
public void start() {
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
Country c = new Country();
c.setName(line);
countries.add(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Init
public void start() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader()
.getResourceAsStream(fileName)));
String line;
while ((line = reader.readLine()) != null) {
Country c = new Country();
c.setName(line);
countries.add(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public List<Country> getStartingWith(String text) {
List<Country> result = new ArrayList<Country>();
for (Country c : countries) {
if (c.getName().toLowerCase().startsWith(text.toLowerCase())) {
result.add(c);
}
}
return result;
}
@Override
public List<Country> getStartingWith(String text) {
List<Country> result = new ArrayList<Country>();
for (Country c : countries) {
if (c.getName().toLowerCase().startsWith(text.toLowerCase())) {
result.add(c);
}
}
return result;
}
}

View file

@ -35,7 +35,7 @@
<component name="CountryRepositoryComponent">
<implementation.java
class="org.apache.tuscany.sca.sample.comet.CountryRepositoryImpl" />
<property name="fileName">/home/fmoga/coding/apache-tuscany/contrib/samples/learning-more/binding-comet/single-response-webapp/src/main/webapp/WEB-INF/countries.txt</property>
<property name="fileName">countries.txt</property>
</component>
</composite>