Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAY-2510 Create builder to load custom modules into plugins and modeler #360

Merged
merged 1 commit into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Changes/New Features:
CAY-2467 New type-aware Property API
CAY-2507 Property API to use path aliases
CAY-2508 Create api to add aliases in expressions
CAY-2510 Create builder to load custom modules into plugins and modeler

Bug Fixes:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@
****************************************************************/
package org.apache.cayenne.tools;

import java.io.File;

import foundrylogic.vpp.VPPConfig;
import org.apache.cayenne.configuration.xml.DataChannelMetaData;
import org.apache.cayenne.dbsync.filter.NamePatternMatcher;
import org.apache.cayenne.dbsync.reverse.configuration.ToolsModule;
import org.apache.cayenne.di.DIBootstrap;
import org.apache.cayenne.di.Injector;
import org.apache.cayenne.gen.ArtifactsGenerationMode;
import org.apache.cayenne.gen.CgenConfiguration;
import org.apache.cayenne.gen.CgenModule;
import org.apache.cayenne.gen.ClassGenerationAction;
import org.apache.cayenne.gen.ClassGenerationActionFactory;
import org.apache.cayenne.gen.ClientClassGenerationAction;
import org.apache.cayenne.map.DataMap;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Path;
import org.apache.velocity.VelocityContext;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
* An Ant task to perform class generation based on CayenneDataMap.
*
Expand Down Expand Up @@ -104,7 +103,9 @@ protected VelocityContext getVppContext() {
public void execute() throws BuildException {
validateAttributes();

injector = DIBootstrap.createInjector(new CgenModule(), new ToolsModule(LoggerFactory.getLogger(CayenneGeneratorTask.class)));
injector = new ToolsInjectorBuilder()
.addModule(new ToolsModule(LoggerFactory.getLogger(CayenneGeneratorTask.class)))
.create();

logger = new AntLogger(this);
CayenneGeneratorMapLoaderAction loadAction = new CayenneGeneratorMapLoaderAction(injector);
Expand Down Expand Up @@ -144,11 +145,7 @@ public void execute() throws BuildException {

private ClassGenerationAction createGenerator(DataMap dataMap) {
CgenConfiguration cgenConfiguration = buildConfiguration(dataMap);
ClassGenerationAction classGenerationAction = cgenConfiguration.isClient() ? new ClientClassGenerationAction(cgenConfiguration) :
new ClassGenerationAction(cgenConfiguration);
injector.injectMembers(classGenerationAction);

return classGenerationAction;
return injector.getInstance(ClassGenerationActionFactory.class).createAction(cgenConfiguration);
}

private boolean hasConfig() {
Expand Down
8 changes: 8 additions & 0 deletions cayenne-cgen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.cayenne</groupId>
<artifactId>cayenne-server</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public class CgenModule implements Module{
@Override
public void configure(Binder binder) {
binder.bind(ClassGenerationActionFactory.class).to(DefaultClassGenerationActionFactory.class);
ProjectModule.contributeExtensions(binder).add(CgenExtension.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/

package org.apache.cayenne.gen;

import java.util.Collection;
import java.util.Collections;

import org.apache.cayenne.di.Module;
import org.apache.cayenne.tools.CayenneToolsModuleProvider;

/**
* @since 4.2
*/
public class CgenToolsModuleProvider implements CayenneToolsModuleProvider {
@Override
public Module module() {
return new CgenModule();
}

@Override
public Class<? extends Module> moduleType() {
return CgenModule.class;
}

@Override
public Collection<Class<? extends Module>> overrides() {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@

package org.apache.cayenne.gen;

import org.apache.cayenne.CayenneRuntimeException;
import org.apache.cayenne.map.Embeddable;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.map.QueryDescriptor;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -42,6 +33,15 @@
import java.util.Properties;
import java.util.stream.Collectors;

import org.apache.cayenne.CayenneRuntimeException;
import org.apache.cayenne.map.Embeddable;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.map.QueryDescriptor;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;

public class ClassGenerationAction {

private static final String TEMPLATES_DIR_NAME = "templates/v4_1/";
Expand Down Expand Up @@ -71,10 +71,9 @@ public class ClassGenerationAction {
protected VelocityContext context;
protected Map<String, Template> templateCache;

public ClassGenerationAction(CgenConfiguration cgenConfiguration) {
public ClassGenerationAction() {
this.context = new VelocityContext();
this.templateCache = new HashMap<>(5);
this.cgenConfiguration = cgenConfiguration;
}

public String defaultTemplateName(TemplateType type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/
package org.apache.cayenne.gen;

/**
* @since 4.2
*/
public interface ClassGenerationActionFactory {

ClassGenerationAction createAction(CgenConfiguration cgenConfiguration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

package org.apache.cayenne.gen;

import java.util.Collection;

import org.apache.cayenne.CayenneRuntimeException;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.map.QueryDescriptor;

import java.util.Collection;

/**
* @since 3.0
*/
Expand All @@ -41,12 +41,8 @@ public class ClientClassGenerationAction extends ClassGenerationAction {

public static final String CLIENT_SUPERCLASS_PREFIX = "_Client";

public ClientClassGenerationAction(CgenConfiguration cgenConfiguration) {
super(cgenConfiguration);
cgenConfiguration.setTemplate(SUBCLASS_TEMPLATE);
cgenConfiguration.setSuperTemplate(SUPERCLASS_TEMPLATE);
cgenConfiguration.setQueryTemplate(DMAP_SUBCLASS_TEMPLATE);
cgenConfiguration.setQuerySuperTemplate(DMAP_SUPERCLASS_TEMPLATE);
public ClientClassGenerationAction() {
super();
}

@Override
Expand Down Expand Up @@ -103,4 +99,12 @@ public void addQueries(Collection<QueryDescriptor> queries) {
}
}
}

public void setCgenConfiguration(CgenConfiguration cgenConfiguration) {
super.setCgenConfiguration(cgenConfiguration);
cgenConfiguration.setTemplate(SUBCLASS_TEMPLATE);
cgenConfiguration.setSuperTemplate(SUPERCLASS_TEMPLATE);
cgenConfiguration.setQueryTemplate(DMAP_SUBCLASS_TEMPLATE);
cgenConfiguration.setQuerySuperTemplate(DMAP_SUPERCLASS_TEMPLATE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/

package org.apache.cayenne.gen;

/**
* @since 4.2
*/
public class DefaultClassGenerationActionFactory implements ClassGenerationActionFactory {

@Override
public ClassGenerationAction createAction(CgenConfiguration cgenConfiguration) {
ClassGenerationAction classGenerationAction = cgenConfiguration.isClient() ?
new ClientClassGenerationAction() :
new ClassGenerationAction();
classGenerationAction.setCgenConfiguration(cgenConfiguration);
return classGenerationAction;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/

package org.apache.cayenne.tools;

import org.apache.cayenne.di.spi.ModuleProvider;

/**
* @since 4.2
*/
public interface CayenneToolsModuleProvider extends ModuleProvider {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/
package org.apache.cayenne.tools;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.cayenne.di.DIBootstrap;
import org.apache.cayenne.di.Injector;
import org.apache.cayenne.di.Module;
import org.apache.cayenne.di.spi.ModuleLoader;

/**
* @since 4.2
*/
public class ToolsInjectorBuilder {

private Collection<Module> modules;

public ToolsInjectorBuilder() {
this.modules = new ArrayList<>();
}

public ToolsInjectorBuilder addModule(Module module) {
modules.add(module);
return this;
}

public ToolsInjectorBuilder addModules(Collection<Module> modules) {
this.modules.addAll(modules);
return this;
}

private Collection<? extends Module> autoLoadedModules() {
return new ModuleLoader().load(CayenneToolsModuleProvider.class);
}

public Injector create() {
Collection<Module> allModules = new ArrayList<>(autoLoadedModules());
allModules.addAll(modules);
return DIBootstrap.createInjector(allModules);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
##################################################################
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
##################################################################

org.apache.cayenne.gen.CgenToolsModuleProvider