Skip to content

Instantly share code, notes, and snippets.

@Mugi4ok
Created April 16, 2015 15:38
Show Gist options
  • Save Mugi4ok/86332ae739454676b08e to your computer and use it in GitHub Desktop.
Save Mugi4ok/86332ae739454676b08e to your computer and use it in GitHub Desktop.
WinAPIAxisRPC
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.server.ServiceLifecycle;
import javax.xml.rpc.server.ServletEndpointContext;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.server.ServiceLifecycle;
import org.apache.axis.MessageContext;
import org.apache.axis.session.SimpleSession;
import javax.servlet.*;
/**
* Created by mugi4_000 on 04.02.15.
*/
public class WinAPIAxisRPC implements ServiceLifecycle {
public WinAPIAxisRPC() {}
public static final String DB_CONNECTION = "dbconnection";
ServletContext sc;
@Override
public void init(Object o) throws ServiceException {
try {
ServletEndpointContext context = (ServletEndpointContext) o;
Class.forName("com.mysql.jdbc.Driver");
// Creating the DB connection for the sample DB
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/winapiref",
"root", "");
System.out.println("startUp connection created");
//Storing the DB in the ConfigurationContext
sc = context.getServletContext();
sc.setAttribute(DB_CONNECTION, conn);
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error in startUp");
}
}
@Override
public void destroy() {
Connection conn = (Connection) sc.getAttribute(DB_CONNECTION);
if (conn != null) {
try {
// closing the DB
conn.close();
System.out.println("shutDown closed connection");
} catch (SQLException e) {
System.out.println("Error while closing the DB connection");
}
} }
public boolean createFunction (String name, String description, String returnType) throws SQLException {
PreparedStatement statement = null;
PreparedStatement getIdStatement = null;
Connection connection = (Connection) sc.getAttribute(DB_CONNECTION);
if(connection != null)
try {
String sql = "INSERT INTO FUNCTIONS (name, description, return_type) VALUES (?, ?, ?)";
statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, description);
statement.setString(3, returnType);
statement.executeUpdate();
return true;
}
finally {
if(statement != null) statement.close();
}
return false;
}
public boolean updateFunction (int id, String name, String description, String returnType) throws SQLException {
PreparedStatement statement = null;
Connection connection = (Connection) sc.getAttribute(DB_CONNECTION);
if(connection != null)
try {
String sql = "UPDATE FUNCTIONS SET name = ?, description = ?, return_type = ? WHERE id = ?";
statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, description);
statement.setString(3, returnType);
statement.setInt(4, id);
statement.executeUpdate();
return true;
}
finally {
if(statement != null) statement.close();
}
return false;
}
public boolean deleteFunction (int key) throws SQLException {
PreparedStatement statement = null;
Connection connection = (Connection) sc.getAttribute(DB_CONNECTION);
if(connection != null)
try {
String sql = "DELETE FROM FUNCTIONS WHERE id = ?";
statement = connection.prepareStatement(sql);
statement.setInt(1, key);
statement.executeUpdate();
return true;
}
finally {
if(statement != null) statement.close();
}
return false;
}
public RefObject getFunctionByPK(int key) throws SQLException {
PreparedStatement statement = null;
System.out.println("Getting into getFunctionByPK");
Connection connection = (Connection) sc.getAttribute(DB_CONNECTION);
if (connection == null)
System.out.println("Connection is NULL");
RefObject refObject = null;
if(connection != null){
try {
System.out.println("Connection established");
Params[] params = new Params[0];
System.out.println("Geting Params[]");
Params[] temp = getAllParameters(key);
System.out.println("Params[] Extracted");
if(temp != null)
params = temp;
String sql = "SELECT * FROM FUNCTIONS WHERE id = ?";
statement = connection.prepareStatement(sql);
statement.setInt(1, key);
ResultSet resultSet = statement.executeQuery();
if (resultSet.isBeforeFirst())
{
resultSet.next();
refObject = new RefObject(resultSet.getInt("id"), resultSet.getString("name"),
resultSet.getString("description"), resultSet.getString("return_type"), params);
if(refObject == null) System.out.println("RefObject is null");
else
System.out.println(refObject.getId() + " " + refObject.getName());
return refObject;
}
else System.out.println("No resultSet present");
}
finally {
if(statement != null) statement.close();
}
}
else
System.out.println("No connections");
return refObject;
}
public RefObject[] getAll() throws SQLException {
PreparedStatement statement = null;
Connection connection = (Connection) sc.getAttribute(DB_CONNECTION);
RefObject refObject = null;
List<RefObject> list = null;
if(connection != null)
try {
String sql = "SELECT * FROM FUNCTIONS";
statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
if (resultSet.isBeforeFirst())
{
list = new ArrayList<RefObject>();
while (resultSet.next()) {
Params[] params = new Params[0];
Params[] temp = getAllParameters(resultSet.getInt("id"));
if(temp != null)
params = temp;
refObject = new RefObject(resultSet.getInt("id"), resultSet.getString("name"),
resultSet.getString("description"), resultSet.getString("return_type"), params);
list.add(refObject);
}
return (RefObject[]) list.toArray(new RefObject[list.size()]);
}
}
finally {
if(statement != null) statement.close();
}
return null;
}
public boolean createParameter (String param, int fId) throws SQLException {
PreparedStatement statement = null;
PreparedStatement getIdStatement = null;
Connection connection = (Connection) sc.getAttribute(DB_CONNECTION);
if(connection != null)
try {
String sql = "INSERT INTO PARAMETERS(id, f_id, param) VALUES (NULL, ?, ?)";
statement = connection.prepareStatement(sql);
statement.setInt(1, fId);
statement.setString(2, param);
statement.executeUpdate();
return true;
}
finally {
if(statement != null) statement.close();
}
return false;
}
public boolean updateParameter(int id, String param, int fId) throws SQLException {
PreparedStatement statement = null;
Connection connection = (Connection) sc.getAttribute(DB_CONNECTION);
if(connection != null)
try {
String sql = "UPDATE PARAMETERS SET f_id = ?, param = ? WHERE id = ?";
statement = connection.prepareStatement(sql);
statement.setInt(1, fId);
statement.setString(2, param);
statement.setInt(3, id);
statement.executeUpdate();
return true;
}
finally {
if(statement != null) statement.close();
}
return false;
}
public boolean deleteParameter(int key) throws SQLException {
PreparedStatement statement = null;
Connection connection = (Connection) sc.getAttribute(DB_CONNECTION);
if(connection != null)
try {
String sql = "DELETE FROM PARAMETERS WHERE id = ?";
statement = connection.prepareStatement(sql);
statement.setInt(1, key);
statement.executeUpdate();
return true;
}
finally {
if(statement != null) statement.close();
}
return false;
}
private Params[] getAllParameters(int fId) throws SQLException {
PreparedStatement statement = null;
RefObject refObject = null;
Connection connection = (Connection) sc.getAttribute(DB_CONNECTION);
if(connection != null)
try {
System.out.println("Got into getAllParameters with Connection");
String sql = "SELECT * FROM PARAMETERS WHERE f_id = ?";
statement = connection.prepareStatement(sql);
statement.setInt(1, fId);
System.out.println("Preparing for query exec");
ResultSet resultSet = statement.executeQuery();
System.out.println("Query passed");
if (resultSet.isBeforeFirst())
{
System.out.println("Entered resultSet");
refObject = new RefObject(1, "1", "1", "1", new Params[0]);
if (refObject == null) System.out.println("RefObject is null");
int id;
String param;
System.out.println("RefObject created");
while (resultSet.next()) {
System.out.println("Trying to get id from resultSet");
id = resultSet.getInt("id");
System.out.println("id = " + id + " Trying to get param from resultSet");
param = resultSet.getString("param");
System.out.println("param = " + param + " Setting parameters");
refObject.setParam(id, param);
}
System.out.println("All objects retrieved, returning");
return refObject.getParams();
}
else System.out.println("No resultSet in getAllParameters");
}
finally {
if(statement != null) statement.close();
}
return null;
}
public RefObject testMethod () {
RefObject refObject = new RefObject();
refObject.setId(1);
refObject.setName("name");
refObject.setDescription("description");
refObject.setReturnType("returnType");
return refObject;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment