Skip to content
Open

list #91

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
1 change: 1 addition & 0 deletions Owner.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nagaiko Ivan
889 changes: 889 additions & 0 deletions ejb3-persistence-1.0.2.GA.jar

Large diffs are not rendered by default.

Binary file added logs/application.log
Binary file not shown.
81 changes: 75 additions & 6 deletions src/main/java/track/container/Container.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,103 @@
package track.container;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
import track.container.config.Bean;
import track.container.config.InvalidConfigurationException;
import track.container.config.Property;
import track.container.config.ValueType;

import static org.apache.log4j.config.PropertyPrinter.capitalize;

/**
* Основной класс контейнера
* У него определено 2 публичных метода, можете дописывать свои методы и конструкторы
*/
public class Container {

private Map<String, Bean> beanById;
private Map<String, Bean> beanByClassName;
private Map<Bean, Object> objById;

// Реализуйте этот конструктор, используется в тестах!
public Container(List<Bean> beans) {

beanById = new HashMap<>();
beanByClassName = new HashMap<>();
objById = new HashMap<>();
for (Bean bean : beans) {
beanById.put(bean.getId(), bean);
beanByClassName.put(bean.getClassName(), bean);
}
}

/**
* Вернуть объект по имени бина из конфига
* Например, Car car = (Car) container.getById("carBean")
*/
public Object getById(String id) {
return null;
public Object getById(String id) throws InvalidConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
Object object = objById.get(id);
if (object == null) {
Bean bean = beanById.get(id);
if (bean != null) {
object = createObject(bean);
} else {
throw new InvalidConfigurationException("Bean without id");
}
}

return object;
}

/**
* Вернуть объект по имени класса
* Например, Car car = (Car) container.getByClass("track.container.beans.Car")
*/
public Object getByClass(String className) {
return null;
public Object getByClass(String className) throws InvalidConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
Object object = objById.get(className);

if (object == null) {
Bean bean = beanByClassName.get(className);
if (bean != null) {
object = createObject(bean);
} else {
throw new InvalidConfigurationException("Bean without class");
}
}

return object;
}

private Object createObject(Bean bean) throws ValueException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
Class clazz = Class.forName(bean.getClassName());
Object object = clazz.newInstance();
objById.put(bean, object);
for (Map.Entry<String, Property> entry : bean.getProperties().entrySet()) {
Property property = entry.getValue();
Field field = clazz.getDeclaredField(property.getName());
Class type = field.getType();
Method set = clazz.getMethod(getSetName(property.getName()), type);
if (property.getType() == ValueType.VAL) {
set.invoke(object, Integer.parseInt(property.getValue()));
} else if (property.getType() == ValueType.REF) {
if (!objById.containsKey(beanById.get(property.getValue()))) {
createObject(beanById.get(property.getValue()));
}
set.invoke(object, objById.get(beanById.get(property.getValue())));
}

}
return object;
}

private String getSetName(String line) {
return "set" + Character.toUpperCase(line.charAt(0)) + line.substring(1);
}
}
}
49 changes: 44 additions & 5 deletions src/main/java/track/container/JsonConfigReader.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
package track.container;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import track.container.config.Bean;
import track.container.config.ConfigReader;
import track.container.config.InvalidConfigurationException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import track.container.config.*;

/**
* TODO: Реализовать
*/
public class JsonConfigReader implements ConfigReader {

private Property parseProperty(JsonNode property) throws InvalidConfigurationException {
String name = property.get("name").asText();
ValueType type;
String value = property.get("value").asText();
if (property.get("type").asText().equals("REF")) {
type = ValueType.REF;
} else {
type = ValueType.VAL;
}

return new Property(name, value, type);
}

private Bean parseBean(JsonNode bean) throws InvalidConfigurationException {
String id = bean.get("id").asText();
String className = bean.get("className").asText();
Map<String, Property> properties = new HashMap();
for (JsonNode property: bean.get("properties")) {
Property parsedProperty = parseProperty(property);
properties.put(parsedProperty.getName(), parsedProperty);
}

return new Bean(id, className, properties);
}

@Override
public List<Bean> parseBeans(File configFile) throws InvalidConfigurationException {
return null;
ObjectMapper objectMapper = new ObjectMapper();
List<Bean> beans = new ArrayList<>();
try {
JsonNode root = objectMapper.readTree(configFile);
for (JsonNode bean : root.get("beans")) {
beans.add(parseBean(bean));
}
} catch (InvalidConfigurationException | IOException e) {
e.printStackTrace();
}
return beans;
}
}
}
33 changes: 23 additions & 10 deletions src/main/java/track/container/Main.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
package track.container;

import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
import track.container.config.Bean;
import track.container.config.ConfigReader;
import track.container.config.InvalidConfigurationException;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import static org.apache.log4j.helpers.Loader.getResource;

/**
*
*/
public class Main {

public static void main(String[] args) {
public static void main(String[] args) throws ValueException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, InvalidConfigurationException {

/*

ПРИМЕР ИСПОЛЬЗОВАНИЯ

*/

// // При чтении нужно обработать исключение
// ConfigReader reader = new JsonReader();
// List<Bean> beans = reader.parseBeans("config.json");
// Container container = new Container(beans);
//
// Car car = (Car) container.getByClass("track.container.beans.Car");
// car = (Car) container.getById("carBean");
File file = new File(getResource("config.json").getFile());
ConfigReader reader = new JsonConfigReader();
try {
List<Bean> beans = reader.parseBeans(file);
System.out.print(beans.toString());

} catch (InvalidConfigurationException exception) {
System.out.println(exception.getMessage());
}



}
}
}
16 changes: 11 additions & 5 deletions src/main/java/track/lessons/lesson2/Document.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package track.lessons.lesson2;

/**
*
*/
public class Document {
String[] tokens;

Document(String[] token) {
this.tokens = token;
}

String[] getTokens() {
return null;
return tokens;
}

int getTokenCount() {
return 0;
return tokens.length;
}

boolean hasToken(String token) {
for (int i = 0; i < tokens.length; i++) {
if (token.equals(tokens[i])) {
return true;
}
}
return false;
}
}
26 changes: 26 additions & 0 deletions src/main/java/track/lessons/lesson2/Document.java~
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package track.lessons.lesson2;

public class Document {
String[] tokens;

Document(String[] token) {
this.tokens = token;
}

String[] getTokens() {
return tokens;
}

int getTokenCount() {
return tokens.length;
}

boolean hasToken(String token) {
for(int i = 0; i < tokens.length; i++) {
if (token.equals(tokens[i])) {
return true;
}
}
return false;
}
}
25 changes: 13 additions & 12 deletions src/main/java/track/lessons/lesson2/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
import java.io.BufferedReader;
import java.io.FileReader;

/**
*
*/
public class Parser {

Document parse(String data) {
return null;
String[] tokens = data.split("[ ]");
if (tokens == null) {
return null;
}
return new Document(tokens);
}

public static void main(String[] args) throws Exception {

String path = "path/to/file";
BufferedReader reader = new BufferedReader(new FileReader(path));
// reader умеет читать по строкам с помощью метода readLine()

// Создайте объект Parser

// Получите объект Document, реализовав метод parse()


StringBuilder builder = new StringBuilder();
Parser pars = new Parser();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String res = builder.toString();
Document doc = pars.parse(res);
}
}
28 changes: 28 additions & 0 deletions src/main/java/track/lessons/lesson2/Parser.java~
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package track.lessons.lesson2;

import java.io.BufferedReader;
import java.io.FileReader;

public class Parser {
Document parse(String data) {
String[] tokens = data.split("[ ]");
if (tokens == null) {
return null;
}
return new Document(tokens);
}

public static void main(String[] args) throws Exception {

String path = "path/to/file";
BufferedReader reader = new BufferedReader(new FileReader(path));
StringBuilder builder = new StringBuilder();
Parser pars = new Parser();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String res = builder.toString();
Document doc = pars.parse(res);
}
}
Loading