001package ball.util.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: PreferencesTask.java 7215 2021-01-03 18:39:51Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/util/ant/taskdefs/PreferencesTask.java $
007 * %%
008 * Copyright (C) 2008 - 2021 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 java.util.prefs.Preferences;
024import lombok.Getter;
025import lombok.NoArgsConstructor;
026import lombok.Setter;
027import lombok.ToString;
028import lombok.experimental.Accessors;
029import org.apache.tools.ant.BuildException;
030import org.apache.tools.ant.Project;
031import org.apache.tools.ant.Task;
032import org.apache.tools.ant.util.ClasspathUtils;
033
034import static lombok.AccessLevel.PROTECTED;
035
036/**
037 * Abstract {@link.uri http://ant.apache.org/ Ant} {@link Task} base class
038 * for {@link Preferences}-specific tasks.
039 *
040 * {@ant.task}
041 *
042 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
043 * @version $Revision: 7215 $
044 */
045@NoArgsConstructor(access = PROTECTED)
046public abstract class PreferencesTask extends Task
047                                      implements AnnotatedAntTask,
048                                                 ClasspathDelegateAntTask,
049                                                 ConfigurableAntTask {
050    @Getter @Setter @Accessors(chain = true, fluent = true)
051    private ClasspathUtils.Delegate delegate = null;
052    @Getter
053    private String type = null;
054
055    public void setType(String string) {
056        type = string;
057        ClasspathDelegateAntTask.super.setClassname(type);
058    }
059
060    @Override
061    public void init() throws BuildException {
062        super.init();
063        ClasspathDelegateAntTask.super.init();
064        ConfigurableAntTask.super.init();
065    }
066
067    @Override
068    public void execute() throws BuildException {
069        super.execute();
070        AnnotatedAntTask.super.execute();
071    }
072
073    /**
074     * {@link Preferences} export {@link Task}.
075     *
076     * {@ant.task}
077     */
078    @NoArgsConstructor @ToString
079    @AntTask("preferences-export")
080    public static class Export extends PreferencesTask {
081        @Override
082        public void execute() throws BuildException {
083            super.execute();
084
085            try {
086                Preferences system = Preferences.systemRoot();
087                Preferences user = Preferences.userRoot();
088                Class<?> type = null;
089
090                if (getType() != null) {
091                    type = getClassForName(getType());
092                    system = Preferences.systemNodeForPackage(type);
093                    user = Preferences.userNodeForPackage(type);
094                }
095
096                system.exportSubtree(System.out);
097                System.out.println("");
098                user.exportSubtree(System.out);
099            } catch (BuildException exception) {
100                throw exception;
101            } catch (RuntimeException exception) {
102                throw exception;
103            } catch (Exception exception) {
104                throw new BuildException(exception);
105            }
106        }
107    }
108}