Skip to content

Commit

Permalink
Fix #8
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Jul 13, 2015
1 parent 0146915 commit f758d64
Show file tree
Hide file tree
Showing 14 changed files with 925 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -31,7 +31,7 @@


<groupId>com.speedment</groupId> <groupId>com.speedment</groupId>
<artifactId>speedment</artifactId> <artifactId>speedment</artifactId>
<version>2.0.0-EA</version> <version>2.0.0-EA2-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Expand Up @@ -72,7 +72,7 @@ public void accept(Project project) {
translators.add(new SpeedmentApplicationTranslator(gen, project)); translators.add(new SpeedmentApplicationTranslator(gen, project));
translators.add(new SpeedmentApplicationMetadataTranslator(gen, project)); translators.add(new SpeedmentApplicationMetadataTranslator(gen, project));


project.traversalOf(Table.class).forEach(table -> { project.traverseOver(Table.class).forEach(table -> {
translators.add(new EntityTranslator(gen, table)); translators.add(new EntityTranslator(gen, table));
translators.add(new EntityBuilderTranslator(gen, table)); translators.add(new EntityBuilderTranslator(gen, table));
translators.add(new EntityImplTranslator(gen, table)); translators.add(new EntityImplTranslator(gen, table));
Expand Down Expand Up @@ -109,7 +109,7 @@ public void accept(Project project) {




List<Table> tables = project List<Table> tables = project
.traversalOf(Table.class) .traverseOver(Table.class)
.collect(toList()); .collect(toList());


gen.metaOn(tables, File.class).forEach(meta -> { gen.metaOn(tables, File.class).forEach(meta -> {
Expand Down
Expand Up @@ -53,7 +53,7 @@ protected Class make(File file) {
.add(OVERRIDE) .add(OVERRIDE)
.add("loadAndSetProject();"); .add("loadAndSetProject();");


project().traversalOf(Table.class).forEachOrdered(t -> { project().traverseOver(Table.class).forEachOrdered(t -> {
EntityManagerImplTranslator entityManagerImplTranslator = new EntityManagerImplTranslator(getCodeGenerator(), t); EntityManagerImplTranslator entityManagerImplTranslator = new EntityManagerImplTranslator(getCodeGenerator(), t);
final Type managerType = entityManagerImplTranslator.getImplType(); final Type managerType = entityManagerImplTranslator.getImplType();
file.add(Import.of(managerType)); file.add(Import.of(managerType));
Expand Down
Expand Up @@ -16,12 +16,12 @@
*/ */
package com.speedment.core.config.model.aspects; package com.speedment.core.config.model.aspects;


import com.speedment.core.config.model.Table;
import java.util.Optional; import java.util.Optional;


/** /**
* *
* @author Emil Forslund * @author Emil Forslund
* @param <P> Parent type
*/ */
public interface Child<P extends Parent<?>> extends Node { public interface Child<P extends Parent<?>> extends Node {


Expand Down
53 changes: 41 additions & 12 deletions src/main/java/com/speedment/core/config/model/aspects/Parent.java
Expand Up @@ -25,10 +25,12 @@
/** /**
* *
* @author Emil Forslund * @author Emil Forslund
* @param <C> Child type
*/ */
public interface Parent<C extends Child<?>> extends Node { public interface Parent<C extends Child<?>> extends Node {

ChildHolder getChildren(); ChildHolder getChildren();

@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
default Optional<C> add(final C child) { default Optional<C> add(final C child) {
return getChildren().put(this, child).map(c -> (C) c); return getChildren().put(this, child).map(c -> (C) c);
Expand All @@ -47,42 +49,69 @@ default boolean contains(final C child) {
default Stream<? extends C> stream() { default Stream<? extends C> stream() {
return getChildren().stream().map(c -> (C) c); return getChildren().stream().map(c -> (C) c);
} }

default <T extends C> Stream<T> streamOf(Class<T> childClass) { default <T extends C> Stream<T> streamOf(Class<T> childClass) {
return getChildren().streamOf(childClass); return getChildren().streamOf(childClass);
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
default <T extends Node> Stream<T> traversalOf(Class<T> childClass) { default <T extends Node> Stream<T> traverseOver(Class<T> childClass) {
return Parent.this.traverse()
.filter(t -> childClass.isAssignableFrom(t.getClass()))
//.filter(t -> childClass.isAssignableFrom(t.getInterfaceMainClass()))
.map(t -> (T) t);
}

@SuppressWarnings("unchecked")
default Stream<Node> traverse() {
final Function<Node, Stream<Node>> traverse = n -> n.asParent() final Function<Node, Stream<Node>> traverse = n -> n.asParent()
.map(p -> p.stream()) .map(p -> p.stream())
.orElse(Stream.empty()) .orElse(Stream.empty())
.map(e -> (Node) e); .map(e -> (Node) e);

return Trees.traverse( return Trees.traverse(
this, this,
traverse, traverse,
Trees.TraversalOrder.BREADTH_FIRST Trees.TraversalOrder.BREADTH_FIRST
).filter(t -> childClass.isAssignableFrom( ).map(Node.class::cast);
t.getInterfaceMainClass()
)).map(t -> (T) t);
} }


default boolean isEmpty() { default boolean isEmpty() {
return getChildren().isEmpty(); return getChildren().isEmpty();
} }

default long size() { default long size() {
return getChildren().size(); return getChildren().size();
} }

@Override @Override
default boolean isParentInterface() { default boolean isParentInterface() {
return true; return true;
} }

@Override @Override
default Optional<Parent<?>> asParent() { default Optional<Parent<?>> asParent() {
return Optional.of(this); return Optional.of(this);
} }
}
default Stream<Child<?>> descendants() {
return Parent.this.traverse().filter(n -> this != n).map(n -> (Child<?>) n);
}

//
// default Stream<Child<?>> descendants() {
// return stream()
// .flatMap((Child child) -> Trees.traverse(child, c -> c.asParent()
// .map(p -> p.stream())
// .orElse(Stream.empty())
// .map(n -> (Child<?>) n),
// Trees.TraversalOrder.DEPTH_FIRST_PRE
// ))
// .map(n -> (Child<?>) n);
// }
//
// default <T extends Child<?>> Stream<T> descendantsOf(Class<T> decendantsClass) {
// return descendants().filter(d -> d.getClass().equals(decendantsClass)).map(decendantsClass::cast);
// }
//
}
Expand Up @@ -16,6 +16,8 @@
*/ */
package com.speedment.core.runtime; package com.speedment.core.runtime;


import com.speedment.core.config.model.ConfigEntity;
import com.speedment.core.config.model.Dbms;
import com.speedment.core.config.model.External; import com.speedment.core.config.model.External;
import com.speedment.core.config.model.Project; import com.speedment.core.config.model.Project;
import com.speedment.core.config.model.aspects.Node; import com.speedment.core.config.model.aspects.Node;
Expand All @@ -33,13 +35,16 @@
import com.speedment.util.Trees; import com.speedment.util.Trees;
import com.speedment.util.analytics.AnalyticsUtil; import com.speedment.util.analytics.AnalyticsUtil;
import static com.speedment.util.analytics.FocusPoint.APP_STARTED; import static com.speedment.util.analytics.FocusPoint.APP_STARTED;
import com.speedment.util.tuple.Tuple2;
import com.speedment.util.tuple.Tuple3;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
Expand All @@ -56,42 +61,159 @@ public abstract class SpeedmentApplicationLifecycle<T extends SpeedmentApplicati


private final static Logger LOGGER = LogManager.getLogger(SpeedmentApplicationLifecycle.class); private final static Logger LOGGER = LogManager.getLogger(SpeedmentApplicationLifecycle.class);


private String dbmsPassword; private final List<Tuple3<Class<? extends ConfigEntity>, String, Consumer<ConfigEntity>>> withsNamed;
private final Map<String, String> dbmsPasswords; private final List<Tuple2<Class<? extends ConfigEntity>, Consumer<ConfigEntity>>> withsAll;

private ApplicationMetadata speedmentApplicationMetadata; private ApplicationMetadata speedmentApplicationMetadata;
private Path configPath; private Path configPath;


public SpeedmentApplicationLifecycle() { public SpeedmentApplicationLifecycle() {
super(); super();
configPath = null; configPath = null;
dbmsPasswords = new ConcurrentHashMap<>(); withsNamed = newList();
withsAll = newList();
}

/**
* Configures a parameter for the named ConfigEntity of a certain class. The
* consumer will then be applied after the configuration has been read and
* after the System properties have been applied.
*
* @param <C> the type of ConfigEntity that is to be used
* @param type the class of the type of ConfigEntity that is to be used
* @param name the fully qualified name of the ConfigEntity.
* @param consumer the consumer to apply.
* @return this instance
*/
public <C extends ConfigEntity> T with(final Class<C> type, final String name, final Consumer<C> consumer) {
@SuppressWarnings("unchecked")
final Consumer<ConfigEntity> consumerCasted = (Consumer<ConfigEntity>) Objects.requireNonNull(consumer);
withsNamed.add(new Tuple3<>(Objects.requireNonNull(type), Objects.requireNonNull(name), consumerCasted));
return self();
}

/**
* Configures a parameter for all ConfigEntity of a certain class. The
* consumer will then be applied after the configuration has been read and
* after the System properties have been applied.
*
* @param <C> the type of ConfigEntity that is to be used
* @param type the class of the type of ConfigEntity that is to be used
* @param consumer the consumer to apply.
* @return this instance
*/
public <C extends ConfigEntity> T with(final Class<C> type, final Consumer<C> consumer) {
@SuppressWarnings("unchecked")
final Consumer<ConfigEntity> consumerCasted = (Consumer<ConfigEntity>) Objects.requireNonNull(consumer);
withsAll.add(new Tuple2<>(Objects.requireNonNull(type), consumerCasted));
return self();
} }


/** /**
* Configures a password for all dbmses in this project. The password will * Configures a password for all dbmses in this project. The password will
* then be applied after the configuration has been read and after the * then be applied after the configuration has been read and after the
* System properties has been applied but before * System properties have been applied.
* configureDbmsPassword(dbmsName, password) is called.
* *
* @param password the password to use for all dbms:es in this project. * @param password to use for all dbms:es in this project.
* @return this instance * @return this instance
*/ */
public T withPassword(final String password) { public T withPassword(final String password) {
dbmsPassword = password; with(Dbms.class, d -> d.setPassword(password));
return self(); return self();
} }


/** /**
* Configures a password for the named dbms. The password will then be * Configures a password for the named dbms. The password will then be
* applied after the configuration has been read and after the System * applied after the configuration has been read and after the System
* properties has been applied. * properties have been applied.
* *
* @param dbmsName the name of the dbms * @param dbmsName the name of the dbms
* @param password the password to use for the named dbms. * @param password to use for the named dbms.
* @return this instance * @return this instance
*/ */
public T withPassword(final String dbmsName, final String password) { public T withPassword(final String dbmsName, final String password) {
dbmsPasswords.put(dbmsName, password); with(Dbms.class, dbmsName, d -> d.setPassword(password));
return self();
}

/**
* Configures a username for all dbmses in this project. The username will
* then be applied after the configuration has been read and after the
* System properties have been applied.
*
* @param username to use for all dbms:es in this project.
* @return this instance
*/
public T withUsername(final String username) {
with(Dbms.class, d -> d.setUsername(username));
return self();
}

/**
* Configures a username for the named dbms. The username will then be
* applied after the configuration has been read and after the System
* properties have been applied.
*
* @param dbmsName the name of the dbms
* @param username to use for the named dbms.
* @return this instance
*/
public T withUsername(final String dbmsName, final String username) {
with(Dbms.class, dbmsName, d -> d.setUsername(username));
return self();
}

/**
* Configures an IP-address for all dbmses in this project. The IP-address
* will then be applied after the configuration has been read and after the
* System properties have been applied.
*
* @param ipAddress to use for all dbms:es in this project.
* @return this instance
*/
public T withIpAddress(final String ipAddress) {
with(Dbms.class, d -> d.setIpAddress(ipAddress));
return self();
}

/**
* Configures an IP-address for the named dbms. The IP-address will then be
* applied after the configuration has been read and after the System
* properties have been applied.
*
* @param dbmsName the name of the dbms
* @param ipAddress to use for the named dbms.
* @return this instance
*/
public T withIpAddress(final String dbmsName, final String ipAddress) {
with(Dbms.class, dbmsName, d -> d.setIpAddress(ipAddress));
return self();
}

/**
* Configures a port for all dbmses in this project. The port will then be
* applied after the configuration has been read and after the System
* properties have been applied.
*
* @param port to use for all dbms:es in this project.
* @return this instance
*/
public T withPort(final int port) {
with(Dbms.class, d -> d.setPort(port));
return self();
}

/**
* Configures a port for the named dbms. The port will then be applied after
* the configuration has been read and after the System properties have been
* applied.
*
* @param dbmsName the name of the dbms
* @param port to use for the named dbms.
* @return this instance
*/
public T withPort(final String dbmsName, final int port) {
with(Dbms.class, dbmsName, d -> d.setPort(port));
return self(); return self();
} }


Expand Down Expand Up @@ -147,14 +269,35 @@ protected void loadAndSetProject() {
); );
}); });


// Apply overidden passwords (if any) for all dbms:es // Apply overidden item (if any) for all ConfigEntities of a given class
Optional.ofNullable(dbmsPassword).ifPresent(pwd -> { withsAll.forEach(t2 -> {
project.stream().forEach(dbms -> dbms.setPassword(pwd)); final Class<? extends ConfigEntity> clazz = t2.get0();
final Consumer<ConfigEntity> consumer = t2.get1();
project.traverse()
.filter(c -> clazz.isAssignableFrom(c.getClass()))
.map(ConfigEntity.class::cast)
.forEachOrdered(n -> consumer.accept(n));
}); });


// Apply overidden passwords (if any) for any named dbms // Apply a named overidden item (if any) for all ConfigEntities of a given class
project.stream().forEach(dbms -> { withsNamed.forEach(t3 -> {
Optional.ofNullable(dbmsPasswords.get(dbms.getName())).ifPresent(pwd -> dbms.setPassword(pwd)); final Class<? extends ConfigEntity> clazz = t3.get0();
final String name = t3.get1();
final Consumer<ConfigEntity> consumer = t3.get2();
project.traverse()
.filter(c -> clazz.isAssignableFrom(c.getClass()))
.map(ConfigEntity.class::cast)
.filter(c -> name.equals(c.getRelativeName(Project.class)))
.forEachOrdered(n -> consumer.accept(n));

// Trees.traverse((Child) project, c -> c.asParent()
// .map(p -> p.stream())
// .orElse(Stream.empty())
// .map(n -> (Child<?>) n),
// Trees.TraversalOrder.DEPTH_FIRST_PRE
// ).filter((Child c) -> clazz.equals(c.getClass()))
// .filter((Child c) -> name.equals(c.getRelativeName(Project.class)))
// .forEachOrdered((Child c) -> consumer.accept(c));
}); });


Platform.get().get(ProjectComponent.class).setProject(project); Platform.get().get(ProjectComponent.class).setProject(project);
Expand Down Expand Up @@ -233,4 +376,8 @@ protected void forEachManagerInSeparateThread(Consumer<Manager<?, ?, ?>> manager


} }


private static <T> List<T> newList() {
return new ArrayList<>();
}

} }

0 comments on commit f758d64

Please sign in to comment.