Skip to content

Commit

Permalink
Merge pull request #21 from waratek/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
stuken committed Feb 3, 2015
2 parents 822dd1f + dc1b9ee commit 4172714
Show file tree
Hide file tree
Showing 37 changed files with 721 additions and 637 deletions.
13 changes: 11 additions & 2 deletions WebContent/conf/Spiracle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ java.url=jdbc:oracle:thin:@localhost:1521:XE
java.username=test
java.password=test

jdbc.fetchsize=25

spring.path=/path/to/spring-context.xml

application.loggingEnabled=True

# Log4j Configuration ##
#Log4j Configuration ##
log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p] [%d{ISO8601}] [%F:%L] [%m]%n
log4j.appender.stdout.layout.ConversionPattern=[%p] [%d{ISO8601}] [%F:%L] [%m]%n

#log4j.rootLogger=DEBUG, FILE
#
#log4j.appender.FILE=org.apache.log4j.FileAppender
#log4j.appender.FILE.File=/tmp/spiracle.log
#log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.FILE.layout.ConversionPattern=[%p] [%d{ISO8601}] [%F:%L] [%m]%n
155 changes: 83 additions & 72 deletions src/com/waratek/spiracle/file/FileServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,79 +36,90 @@
@WebServlet("/FileServlet")
public class FileServlet extends HttpServlet {
private static final Logger logger = Logger.getLogger(FileServlet.class);
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public FileServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeRequest(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeRequest(request, response);
}

private void executeRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();

String method = request.getParameter("fileArg");
String path = request.getParameter("filePath");
String textData = request.getParameter("fileText");

if(method.equals("read")) {
session.setAttribute("fileContents", readFile(path));

} else if(method.equals("write")) {
File f = new File(path);
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(textData);
bw.close();
fw.close();

session.setAttribute("fileContents", readFile(path));

} else if(method.equals("delete")) {
File f = new File(path);
f.delete();
session.setAttribute("fileContents", "");
}
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public FileServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeRequest(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeRequest(request, response);
}

private void executeRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();

String method = request.getParameter("fileArg");
String path = request.getParameter("filePath");
String textData = request.getParameter("fileText");

if(method.equals("read")) {
read(session, path);
} else if(method.equals("write")) {
write(session, path, textData);
} else if(method.equals("delete")) {
delete(session, path);
}

logger.info(method + " " + path + " " + textData);

response.sendRedirect("file.jsp");
}

private String readFile(String pathname) {
try {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int)file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");

try {
while(scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
return fileContents.toString();
} finally {
scanner.close();
}
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
response.sendRedirect("file.jsp");
}

private void delete(HttpSession session, String path) {
File f = new File(path);
f.delete();
session.setAttribute("fileContents", "");
}

private void read(HttpSession session, String path) {
session.setAttribute("fileContents", readFile(path));
}

private void write(HttpSession session, String path, String textData)
throws IOException {
File f = new File(path);
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(textData);
bw.close();
fw.close();

read(session, path);
}

private String readFile(String pathname) {
try {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int)file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");

try {
while(scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
return fileContents.toString();
} finally {
scanner.close();
}
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
}
19 changes: 18 additions & 1 deletion src/com/waratek/spiracle/init/SpiracleInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
Expand All @@ -48,8 +47,10 @@ public void contextInitialized(ServletContextEvent arg0) {
ServletContext application = arg0.getServletContext();
Properties props = loadProperties(application);
loadLog4jConfig(props);
logServerInfo(application);
ComboPooledDataSource ds = getConnectionPool(props);
setConnectionPool(application, ds);
setFetchSize(application, props);
}

private Properties loadProperties(ServletContext application) {
Expand Down Expand Up @@ -113,4 +114,20 @@ private ComboPooledDataSource getConnectionPool(Properties props) {
ds.setMaxPoolSize(maxPoolSize);
return ds;
}

private void setFetchSize(ServletContext application, Properties props) {
int fetchSize = 0;
try {
fetchSize = Integer.parseInt(props.getProperty("jdbc.fetchsize"));
application.setAttribute("fetchSize", fetchSize);
logger.info("Set jdbc.fetchsize to (" + fetchSize + ")");
} catch (NumberFormatException e) {
logger.error("jdbc.fetchsize not specified, default value set(10).");
}
}

void logServerInfo(ServletContext application) {
logger.info("Application Server Name: " + application.getServerInfo());
logger.info("Application Context Path:" + application.getRealPath(""));
}
}
108 changes: 54 additions & 54 deletions src/com/waratek/spiracle/network/ServerSocketServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,62 @@
*/
@WebServlet("/ServerSocketServlet")
public class ServerSocketServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static ServerSocket ss;
private static Socket s;

/**
* @see HttpServlet#HttpServlet()
*/
public ServerSocketServlet() {
super();
// TODO Auto-generated constructor stub
}
private static final long serialVersionUID = 1L;
private static ServerSocket ss;
private static Socket s;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeRequest(request, response);
}
/**
* @see HttpServlet#HttpServlet()
*/
public ServerSocketServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeRequest(request, response);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeRequest(request, response);
}

private void executeRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
if(ss != null && s != null) {
ss.close();
s.close();
}

HttpSession session = request.getSession();
try {
String addr = request.getParameter("hostname");
int port = Integer.parseInt(request.getParameter("port"));
if(addr == null) {
ss = new ServerSocket(port);
} else {
ss = new ServerSocket();
ss.bind(new InetSocketAddress(addr, port));
}
ss.setSoTimeout(20000);
s = ss.accept();
session.setAttribute("serverSocketInfo", ss.toString());
response.sendRedirect("network.jsp");

} catch (Throwable e) {
if(ss != null) {
ss.close();
s.close();
}
e.printStackTrace();
session.setAttribute("serverSocketInfo", e.getMessage());
response.sendRedirect("network.jsp");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeRequest(request, response);
}

private void executeRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
if(ss != null && s != null) {
ss.close();
s.close();
}

HttpSession session = request.getSession();
try {
String addr = request.getParameter("hostname");
int port = Integer.parseInt(request.getParameter("port"));
if(addr == null) {
ss = new ServerSocket(port);
} else {
ss = new ServerSocket();
ss.bind(new InetSocketAddress(addr, port));
}
ss.setSoTimeout(20000);
s = ss.accept();
session.setAttribute("serverSocketInfo", ss.toString());
response.sendRedirect("network.jsp");

} catch (Throwable e) {
if(ss != null) {
ss.close();
s.close();
}
e.printStackTrace();
session.setAttribute("serverSocketInfo", e.getMessage());
response.sendRedirect("network.jsp");
}
}

}
Loading

0 comments on commit 4172714

Please sign in to comment.