View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.dao;
22  
23  import org.waarp.common.database.ConnectionFactory;
24  import org.waarp.openr66.dao.database.DBDAOFactory;
25  import org.waarp.openr66.dao.exception.DAOConnectionException;
26  import org.waarp.openr66.dao.xml.XMLDAOFactory;
27  
28  import javax.xml.XMLConstants;
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import javax.xml.parsers.ParserConfigurationException;
31  import javax.xml.stream.XMLInputFactory;
32  
33  /**
34   * Abstract class to create DAOFactory
35   */
36  public abstract class DAOFactory {
37  
38    private static DAOFactory instance;
39  
40    public static void initialize() {
41      if (instance == null) {
42        instance = new XMLDAOFactory();
43      }
44    }
45  
46    public static void initialize(final ConnectionFactory factory) {
47      if (instance == null) {
48        instance = new DBDAOFactory(factory);
49      }
50    }
51  
52    public static DAOFactory getInstance() {
53      return instance;
54    }
55  
56    /**
57     * Test only
58     *
59     * @param daoFactory
60     */
61    public static void setInstanceTestOnly(DAOFactory daoFactory) {
62      instance = daoFactory;
63    }
64  
65    /**
66     * OWASP security
67     *
68     * @return the {@link DocumentBuilderFactory} ready
69     */
70    public static DocumentBuilderFactory getDocumentBuilderFactory() {
71      final DocumentBuilderFactory factory = // NOSONAR
72          DocumentBuilderFactory.newInstance(); // NOSONAR
73      // disable external entities
74      try {
75        factory.setFeature(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
76                           Boolean.FALSE);
77      } catch (final ParserConfigurationException ignored) {
78        // nothing
79      } catch (final AbstractMethodError ignored) {
80        // nothing
81      }
82      try {
83        factory.setFeature(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
84      } catch (final ParserConfigurationException ignored) {
85        // nothing
86      } catch (final AbstractMethodError ignored) {
87        // nothing
88      }
89      try {
90        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
91      } catch (final ParserConfigurationException ignored) {
92        // nothing
93      } catch (final AbstractMethodError ignored) {
94        // nothing
95      }
96      return factory;
97    }
98  
99    /**
100    * Close the DAO
101    *
102    * @param dao
103    */
104   public static void closeDAO(final AbstractDAO<?> dao) {
105     if (dao != null) {
106       dao.close();
107     }
108   }
109 
110   /**
111    * @return the current configuration of the database maximum connections
112    */
113   public int getMaxConnections() {
114     return 1000;
115   }
116 
117   /**
118    * Return a BusinessDAO
119    *
120    * @param isCacheable
121    *
122    * @return a ready to use BusinessDAO
123    *
124    * @throws DAOConnectionException if cannot create the DAO
125    */
126   public abstract BusinessDAO getBusinessDAO(final boolean isCacheable)
127       throws DAOConnectionException;
128 
129   /**
130    * Return a HostDAO
131    *
132    * @param isCacheable
133    *
134    * @return a ready to use HostDAO
135    *
136    * @throws DAOConnectionException if cannot create the DAO
137    */
138   public abstract HostDAO getHostDAO(final boolean isCacheable)
139       throws DAOConnectionException;
140 
141   /**
142    * Return a LimitDAO
143    *
144    * @param isCacheable
145    *
146    * @return a ready to use LimitDAO
147    *
148    * @throws DAOConnectionException if cannot create the DAO
149    */
150   public abstract LimitDAO getLimitDAO(final boolean isCacheable)
151       throws DAOConnectionException;
152 
153   /**
154    * Return a MultipleMonitorDAO
155    *
156    * @param isCacheable
157    *
158    * @return a ready to use MultipleMonitorDAO
159    *
160    * @throws DAOConnectionException if cannot create the DAO
161    */
162   public abstract MultipleMonitorDAO getMultipleMonitorDAO(
163       final boolean isCacheable) throws DAOConnectionException;
164 
165   /**
166    * Return a RuleDAO
167    *
168    * @param isCacheable
169    *
170    * @return a ready to use RuleDAO
171    *
172    * @throws DAOConnectionException if cannot create the DAO
173    */
174   public abstract RuleDAO getRuleDAO(final boolean isCacheable)
175       throws DAOConnectionException;
176 
177   /**
178    * Return a TransferDAO
179    *
180    * @return a ready to use TramsferDAO
181    *
182    * @throws DAOConnectionException if cannot create the DAO
183    */
184   public abstract TransferDAO getTransferDAO() throws DAOConnectionException;
185 
186   /**
187    * @param request
188    * @param limit
189    * @param offset
190    *
191    * @return the request using limit offset
192    */
193   public abstract String getLimitRequest(final String request, final int limit,
194                                          final int offset);
195 }