OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Rev 94 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 /*
15
 * Créé le 18 avr. 2005
16
 */
17
package org.openconcerto.sql;
18
 
19
import org.openconcerto.sql.element.SQLElementDirectory;
20
import org.openconcerto.sql.model.DBFileCache;
21
import org.openconcerto.sql.model.DBItemFileCache;
22
import org.openconcerto.sql.model.DBRoot;
23
import org.openconcerto.sql.model.DBStructureItem;
24
import org.openconcerto.sql.model.DBSystemRoot;
80 ilm 25
import org.openconcerto.sql.model.FieldMapper;
17 ilm 26
import org.openconcerto.sql.model.SQLBase;
27
import org.openconcerto.sql.model.SQLFilter;
28
import org.openconcerto.sql.model.SQLTable;
29
import org.openconcerto.sql.request.SQLFieldTranslator;
30
import org.openconcerto.utils.FileUtils;
31
 
32
import java.io.File;
33
import java.io.IOException;
63 ilm 34
import java.util.concurrent.Executor;
83 ilm 35
import java.util.concurrent.ExecutorService;
63 ilm 36
import java.util.concurrent.Executors;
17 ilm 37
 
83 ilm 38
import net.jcip.annotations.GuardedBy;
39
 
17 ilm 40
/**
41
 * Regroupe les objets nécessaires au framework.
42
 *
43
 * @author Sylvain CUAZ
44
 */
45
public abstract class Configuration {
46
 
47
    public static File getDefaultConfDir() {
48
        return new File(System.getProperty("user.home"), ".java/ilm/sql-config/");
49
    }
50
 
51
    private static Configuration instance;
52
 
53
    public static SQLFieldTranslator getTranslator(SQLTable t) {
54
        return getInstance().getTranslator();
55
    }
56
 
57
    public static Configuration getInstance() {
58
        return instance;
59
    }
60
 
61
    public static final void setInstance(Configuration instance) {
62
        Configuration.instance = instance;
63
    }
64
 
83 ilm 65
    @GuardedBy("this")
66
    private ExecutorService nonInteractiveSQLExecutor;
63 ilm 67
 
17 ilm 68
    public abstract ShowAs getShowAs();
69
 
70
    public abstract SQLBase getBase();
71
 
72
    public abstract DBRoot getRoot();
73
 
74
    public abstract DBSystemRoot getSystemRoot();
75
 
76
    public abstract SQLFilter getFilter();
77
 
78
    public abstract SQLFieldTranslator getTranslator();
79
 
80
    public abstract SQLElementDirectory getDirectory();
81
 
80 ilm 82
    public abstract FieldMapper getFieldMapper();
83
 
17 ilm 84
    public abstract File getWD();
85
 
73 ilm 86
    // abstract :
87
    // - we can't return a default name as we don't know how to localize it
88
    // - avoid that 2 different application share the same name (and perhaps configuration)
89
    public abstract String getAppName();
17 ilm 90
 
73 ilm 91
    /**
92
     * A string that should be unique to an application and this configuration. E.g. allow to store
93
     * different settings for different uses of a same application.
94
     *
95
     * @return a string beginning with {@link #getAppName()}, <code>null</code> if appName is
96
     *         <code>null</code> or empty.
97
     */
17 ilm 98
    public final String getAppID() {
73 ilm 99
        final String appName = this.getAppName();
100
        if (appName == null || appName.length() == 0)
101
            return null;
102
        return appName + getAppIDSuffix();
17 ilm 103
    }
104
 
105
    protected String getAppIDSuffix() {
106
        return "";
107
    }
108
 
109
    public File getConfDir() {
110
        return new File(getDefaultConfDir(), this.getAppID());
111
    }
112
 
113
    /**
114
     * A directory to store data depending on this {@link #getRoot() root}.
115
     *
116
     * @return a directory for this root.
117
     */
118
    public final File getConfDirForRoot() {
119
        return getConfDir(getRoot());
120
    }
121
 
122
    /**
123
     * Move {@link #getConfDir()}/<code>name</code> to {@link #getConfDirForRoot()}/
124
     * <code>name</code> if necessary.
125
     *
126
     * @param name the name of the file or directory to move.
127
     * @return the new file in <code>getConfDirForRoot()</code>.
128
     */
129
    public final File migrateToConfDirForRoot(final String name) {
130
        final File oldFile = new File(this.getConfDir(), name);
131
        final File newFile = new File(this.getConfDirForRoot(), name);
132
        if (oldFile.exists() && !newFile.exists()) {
133
            try {
134
                FileUtils.mkdir_p(newFile.getParentFile());
135
                oldFile.renameTo(newFile);
136
            } catch (IOException e) {
137
                e.printStackTrace();
138
                FileUtils.rmR(oldFile);
139
            }
140
        }
141
        return newFile;
142
    }
143
 
25 ilm 144
    public final File getConfDir(DBStructureItem<?> db) {
17 ilm 145
        return DBItemFileCache.getDescendant(new File(getConfDir(), "dataDepedent"), DBFileCache.getJDBCAncestorNames(db, true));
146
    }
147
 
148
    /**
149
     * Add the showAs, translator and directory of <code>o</code> to this.
150
     *
151
     * @param o the configuration to add.
152
     * @return this.
153
     * @see ShowAs#putAll(ShowAs)
154
     * @see SQLFieldTranslator#putAll(SQLFieldTranslator)
155
     * @see SQLElementDirectory#putAll(SQLElementDirectory)
156
     */
157
    public Configuration add(Configuration o) {
158
        this.getShowAs().putAll(o.getShowAs());
159
        this.getTranslator().putAll(o.getTranslator());
160
        this.getDirectory().putAll(o.getDirectory());
161
        return this;
162
    }
163
 
164
    /**
165
     * Signal that this conf will not be used anymore.
166
     */
83 ilm 167
    public void destroy() {
168
        synchronized (this) {
169
            if (this.nonInteractiveSQLExecutor != null) {
170
                this.nonInteractiveSQLExecutor.shutdown();
171
            }
172
        }
173
    }
63 ilm 174
 
65 ilm 175
    /**
176
     * An executor that should be used for background SQL requests. It can be used to limit the
177
     * concurrent number of database connections (as establishing a connection is expensive and the
178
     * server might have restrictions).
179
     *
180
     * @return a SQL executor.
181
     */
83 ilm 182
    public synchronized final Executor getNonInteractiveSQLExecutor() {
65 ilm 183
        if (this.nonInteractiveSQLExecutor == null) {
184
            this.nonInteractiveSQLExecutor = createNonInteractiveSQLExecutor();
63 ilm 185
        }
65 ilm 186
        return this.nonInteractiveSQLExecutor;
63 ilm 187
    }
188
 
83 ilm 189
    protected ExecutorService createNonInteractiveSQLExecutor() {
63 ilm 190
        return Executors.newFixedThreadPool(2);
191
    }
17 ilm 192
}