Skip to content
This repository was archived by the owner on Sep 27, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/ru/fizteh/fivt/students/torunova/servlet/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ru.fizteh.fivt.students.torunova.servlet;

import ru.fizteh.fivt.students.torunova.servlet.database.actions.Action;
import ru.fizteh.fivt.students.torunova.servlet.database.actions.Start;
import ru.fizteh.fivt.students.torunova.servlet.database.actions.Stop;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.IncorrectDbException;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.IncorrectDbNameException;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.IncorrectFileException;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.TableNotCreatedException;
import ru.fizteh.fivt.students.torunova.servlet.interpreter.Shell;
import ru.fizteh.fivt.students.torunova.servlet.server.HttpServer;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
* Created by nastya on 22.10.14.
*/

public class Main {
private static final String DATABASE_DIRECTORY = "fizteh.db.dir";

public static void main(String[] args) {
if (System.getProperty(DATABASE_DIRECTORY) == null) {
System.err.println("Name of database not specified. Please, specify it via -D" + DATABASE_DIRECTORY);
System.exit(1);
}
HttpServer server = null;
try {
server = new HttpServer(System.getProperty(DATABASE_DIRECTORY));
} catch (IncorrectDbException | IncorrectDbNameException
| IncorrectFileException | TableNotCreatedException | IOException e) {
System.out.println(e.toString());
System.exit(1);
}
Set<Action> actions = new HashSet<>();
actions.add(new Start(server, System.out));
actions.add(new Stop(server, System.out));
Shell shell = new Shell(actions, System.in, System.out, "stophttp", true);

if (!shell.run()) {
System.exit(1);
} else {
System.exit(0);
}
}
}


123 changes: 123 additions & 0 deletions src/ru/fizteh/fivt/students/torunova/servlet/ProxyFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package ru.fizteh.fivt.students.torunova.servlet;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ru.fizteh.fivt.proxy.LoggingProxyFactory;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.Writer;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.IdentityHashMap;

/**
* Created by nastya on 02.12.14.
*/
public class ProxyFactory implements LoggingProxyFactory {
class Logger implements InvocationHandler {
private Object implementation;
private Document document;
private Transformer transformer;
private final Writer writer;
public Logger(Object newImplementation, Writer newWriter) {
implementation = newImplementation;
document = createNewDocument();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
try {
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
//ignored.
}
writer = newWriter;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
document = createNewDocument();
Element invoke = document.createElement("invoke");
invoke.setAttribute("timestamp", String.valueOf(System.currentTimeMillis()));
invoke.setAttribute("class", implementation.getClass().getName());
invoke.setAttribute("name", method.getName());
document.appendChild(invoke);
Element arguments = document.createElement("arguments");
invoke.appendChild(arguments);
Element argument;
if (args != null) {
for (Object obj : args) {
argument = document.createElement("argument");
if (obj instanceof Iterable) {
IdentityHashMap map = new IdentityHashMap();
map.put(obj, null);
logIterable(argument, (Iterable) obj, map);
} else {
argument.appendChild(document.createTextNode(obj.toString()));
}
arguments.appendChild(argument);
}
}
Element returnTag = document.createElement("return");
Object result = null;
try {
result = method.invoke(implementation, args);
} catch (InvocationTargetException e) {
Element thrown = document.createElement("thrown");
thrown.appendChild(document.createTextNode(e.getTargetException().toString()));
invoke.appendChild(thrown);
transformer.transform(new DOMSource(document), new StreamResult(writer));
throw e.getTargetException();

}
if (method.getReturnType() != void.class) {
returnTag.appendChild(document.createTextNode(result.toString()));
}
invoke.appendChild(returnTag);
synchronized (writer) {
transformer.transform(new DOMSource(document), new StreamResult(writer));
}
return result;
}
private void logIterable(Element parent, Iterable iterable, IdentityHashMap elements) {
Element value;
Element list = document.createElement("list");
parent.appendChild(list);
for (Object obj: iterable) {
value = document.createElement("value");
if (elements.containsKey(obj)) {
value.appendChild(document.createTextNode("cyclic"));
} else if (obj instanceof Iterable) {
IdentityHashMap map = new IdentityHashMap(elements);
map.put(obj, null);
logIterable(value, (Iterable) obj, map);
} else {
value.appendChild(document.createTextNode(obj.toString()));
}
list.appendChild(value);
}

}
private Document createNewDocument() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
//ignored.
}
return builder.newDocument();
}
}
@Override
public Object wrap(Writer writer, Object implementation, Class<?> interfaceClass) {
return Proxy.newProxyInstance(interfaceClass.getClassLoader(),
new Class[] {interfaceClass}, new Logger(implementation, writer));
}
}
171 changes: 171 additions & 0 deletions src/ru/fizteh/fivt/students/torunova/servlet/database/Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package ru.fizteh.fivt.students.torunova.servlet.database;


import ru.fizteh.fivt.storage.strings.TableProvider;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.IncorrectDbException;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.IncorrectDbNameException;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.IncorrectFileException;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.TableNotCreatedException;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
* Created by nastya on 19.10.14.
*/
public class Database implements TableProvider {
private String dbName;
private Map<String, TableImpl> tables = new HashMap<>();

@Override
public int hashCode() {
return dbName.hashCode();
}

@Override
public boolean equals(Object db1) {
if (!(db1 instanceof Database)) {
return false;
}
Database db = (Database) db1;
return dbName.equals(db.dbName);
}

public Database(String name) throws IncorrectDbNameException,
IOException,
TableNotCreatedException,
IncorrectFileException,
IncorrectDbException {
File db = new File(name).getAbsoluteFile();
if (!db.exists()) {
if (!db.mkdirs()) {
throw new RuntimeException("database cannot be created");
}
} else if (!db.isDirectory()) {
throw new IncorrectDbNameException("File with this name already exists.");
}
dbName = db.getAbsolutePath();
File[]dbTables = db.listFiles();
if (dbTables != null) {
for (File table : dbTables) {
if (table.getAbsoluteFile().isDirectory()) {
tables.put(table.getName(), new TableImpl(table.getAbsolutePath()));
} else {
throw new IncorrectDbException("Database contains illegal files.");
}
}
}
}

@Override
public TableImpl getTable(String name) {
checkTableName(name);
return tables.get(name);
}

@Override
public TableImpl createTable(String tableName) {
checkTableName(tableName);
File table = new File(dbName, tableName);
String newTableName = table.getAbsolutePath();
if (!tables.containsKey(tableName)) {
TableImpl newTable;
try {
newTable = new TableImpl(newTableName);
} catch (TableNotCreatedException | IncorrectFileException | IOException e) {
throw new RuntimeException(e);
}
tables.put(tableName, newTable);
return newTable;
}
return null;
}

@Override
public void removeTable(String name) {
checkTableName(name);
File f = new File(dbName, name);
if (tables.containsKey(name)) {
removeRecursive(f.getAbsolutePath());
tables.get(name).markAsRemoved();
tables.remove(name);
} else {
throw new IllegalStateException("does not exist");
}
}

public Map<String, Integer> showTables() {
Map<String, Integer> tablesWithSize = new HashMap<>();
tables.forEach((name, table)->tablesWithSize.put(name, table.getNumberOfEntries()));
return tablesWithSize;
}
public String getDbName() {
return dbName;
}

public void close() {
for (TableImpl t :tables.values()) {
t.rollback();
t.markAsClosed();
}
}
/**
* removes file
* @param file - filename.
* @return true if file is regular and deleted,false otherwise.
*/
private boolean remove(final String file) {
File fileWithAbsolutePath = new File(file).getAbsoluteFile();
if (fileWithAbsolutePath.isFile()) {
if (!fileWithAbsolutePath.delete()) {
return false;
}
} else if (fileWithAbsolutePath.isDirectory()) {
return false;
} else if (!fileWithAbsolutePath.exists()) {
return false;
}
return true;
}
/**
* removes directory.
*
* @param dir - directory name.
*/
private boolean removeRecursive(final String dir) {
File directory = new File(dir).getAbsoluteFile();
if (directory.isDirectory()) {
File[] content = directory.listFiles();
if (content != null) {
for (File item : content) {
if (item.isDirectory()) {
if (!removeRecursive(item.getAbsolutePath())) {
return false;
}
} else {
if (!remove(item.getAbsolutePath())) {
return false;
}
}
}
}
if (!directory.delete()) {
return false;
}
} else {
return false;
}
return true;
}
private void checkTableName(String name) {
if (name == null || Pattern.matches(".*" + Pattern.quote(File.separator) + ".*", name)
|| name.equals("..") || name.equals(".")) {
throw new IllegalArgumentException("illegal table name");
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.fizteh.fivt.students.torunova.servlet.database;

import ru.fizteh.fivt.storage.strings.TableProvider;
import ru.fizteh.fivt.storage.strings.TableProviderFactory;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.IncorrectDbException;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.IncorrectDbNameException;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.TableNotCreatedException;
import ru.fizteh.fivt.students.torunova.servlet.database.exceptions.IncorrectFileException;

import java.io.IOException;

/**
* Created by nastya on 04.11.14.
*/
public class DatabaseFactory implements TableProviderFactory {
public DatabaseFactory(){}
@Override
public TableProvider create(String dir) throws IllegalArgumentException {
TableProvider tp;
if (dir == null) {
throw new IllegalArgumentException("Illegal name of database.");
}
try {
tp = new Database(dir);
} catch (IncorrectDbNameException | IOException | TableNotCreatedException
| IncorrectFileException | IncorrectDbException e) {
throw new RuntimeException(e);
}
return tp;

}
}
Loading