001package ball.persistence.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Persistence Implementation (Hibernate)
005 * $Id: PersistenceTask.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-persistence/trunk/src/main/java/ball/persistence/ant/taskdefs/PersistenceTask.java $
007 * %%
008 * Copyright (C) 2016 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.util.ant.taskdefs.AnnotatedAntTask;
024import ball.util.ant.taskdefs.AntTask;
025import ball.util.ant.taskdefs.ClasspathDelegateAntTask;
026import ball.util.ant.taskdefs.ConfigurableAntTask;
027import ball.util.ant.taskdefs.NotNull;
028import javax.persistence.spi.PersistenceProviderResolver;
029import javax.persistence.spi.PersistenceProviderResolverHolder;
030import lombok.Getter;
031import lombok.NoArgsConstructor;
032import lombok.Setter;
033import lombok.ToString;
034import lombok.experimental.Accessors;
035import org.apache.tools.ant.BuildException;
036import org.apache.tools.ant.Task;
037import org.apache.tools.ant.util.ClasspathUtils;
038
039import static javax.persistence.Persistence.generateSchema;
040import static lombok.AccessLevel.PROTECTED;
041
042/**
043 * Abstract {@link javax.persistence.Persistence}
044 * {@link.uri http://ant.apache.org/ Ant} {@link Task} base class.
045 *
046 * {@bean.info}
047 *
048 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
049 * @version $Revision: 5285 $
050 */
051@NoArgsConstructor(access = PROTECTED)
052public abstract class PersistenceTask extends Task
053                                      implements AnnotatedAntTask,
054                                                 ClasspathDelegateAntTask,
055                                                 ConfigurableAntTask {
056    @Getter @Setter @Accessors(chain = true, fluent = true)
057    private ClasspathUtils.Delegate delegate = null;
058
059    @Override
060    public void init() throws BuildException {
061        super.init();
062        ClasspathDelegateAntTask.super.init();
063        ConfigurableAntTask.super.init();
064    }
065
066    @Override
067    public void execute() throws BuildException {
068        super.execute();
069        AnnotatedAntTask.super.execute();
070    }
071
072    /**
073     * {@link.uri http://ant.apache.org/ Ant} {@link Task} to list available
074     * {@link javax.persistence.spi.PersistenceProvider}s.
075     *
076     * {@bean.info}
077     */
078    @AntTask("list-providers")
079    @NoArgsConstructor @ToString
080    public static class ListProviders extends PersistenceTask {
081        @Override
082        public void execute() throws BuildException {
083            super.execute();
084
085            try {
086                PersistenceProviderResolver resolver =
087                    PersistenceProviderResolverHolder.getPersistenceProviderResolver();
088
089                log(String.valueOf(resolver.getPersistenceProviders()));
090            } catch (BuildException exception) {
091                throw exception;
092            } catch (Throwable throwable) {
093                throwable.printStackTrace();
094                throw new BuildException(throwable);
095            }
096        }
097    }
098
099    /**
100     * {@link.uri http://ant.apache.org/ Ant} {@link Task} to generate
101     * schema.
102     *
103     * {@bean.info}
104     */
105    @AntTask("generate-schema")
106    @NoArgsConstructor @ToString
107    public static class GenerateSchema extends PersistenceTask {
108        @NotNull @Getter @Setter
109        private String name = null;
110
111        @Override
112        public void execute() throws BuildException {
113            super.execute();
114
115            try {
116                generateSchema(getName(), getProject().getProperties());
117            } catch (BuildException exception) {
118                throw exception;
119            } catch (Throwable throwable) {
120                throwable.printStackTrace();
121                throw new BuildException(throwable);
122            }
123        }
124    }
125}