001package ball.util.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Utilities
005 * %%
006 * Copyright (C) 2008 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import java.util.prefs.Preferences;
022import lombok.Getter;
023import lombok.NoArgsConstructor;
024import lombok.Setter;
025import lombok.ToString;
026import lombok.experimental.Accessors;
027import org.apache.tools.ant.BuildException;
028import org.apache.tools.ant.Project;
029import org.apache.tools.ant.Task;
030import org.apache.tools.ant.util.ClasspathUtils;
031
032import static lombok.AccessLevel.PROTECTED;
033
034/**
035 * Abstract {@link.uri http://ant.apache.org/ Ant} {@link Task} base class
036 * for {@link Preferences}-specific tasks.
037 *
038 * {@ant.task}
039 *
040 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
041 */
042@NoArgsConstructor(access = PROTECTED)
043public abstract class PreferencesTask extends Task implements AnnotatedAntTask,
044                                                              ClasspathDelegateAntTask, ConfigurableAntTask {
045    @Getter @Setter @Accessors(chain = true, fluent = true)
046    private ClasspathUtils.Delegate delegate = null;
047    @Getter
048    private String type = null;
049
050    public void setType(String string) {
051        type = string;
052        ClasspathDelegateAntTask.super.setClassname(type);
053    }
054
055    @Override
056    public void init() throws BuildException {
057        super.init();
058        ClasspathDelegateAntTask.super.init();
059        ConfigurableAntTask.super.init();
060    }
061
062    @Override
063    public void execute() throws BuildException {
064        super.execute();
065        AnnotatedAntTask.super.execute();
066    }
067
068    /**
069     * {@link Preferences} export {@link Task}.
070     *
071     * {@ant.task}
072     */
073    @NoArgsConstructor @ToString
074    @AntTask("preferences-export")
075    public static class Export extends PreferencesTask {
076        @Override
077        public void execute() throws BuildException {
078            super.execute();
079
080            try {
081                Preferences system = Preferences.systemRoot();
082                Preferences user = Preferences.userRoot();
083                Class<?> type = null;
084
085                if (getType() != null) {
086                    type = getClassForName(getType());
087                    system = Preferences.systemNodeForPackage(type);
088                    user = Preferences.userNodeForPackage(type);
089                }
090
091                system.exportSubtree(System.out);
092                System.out.println("");
093                user.exportSubtree(System.out);
094            } catch (BuildException exception) {
095                throw exception;
096            } catch (RuntimeException exception) {
097                throw exception;
098            } catch (Exception exception) {
099                throw new BuildException(exception);
100            }
101        }
102    }
103}