View Javadoc
1   /*
2    * Tentackle - https://tentackle.org
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   */
18  
19  package org.tentackle.dbms.trip;
20  
21  import org.tentackle.dbms.AbstractDbOperation;
22  import org.tentackle.dbms.Db;
23  import org.tentackle.session.PersistenceException;
24  
25  import java.lang.reflect.InvocationTargetException;
26  
27  /**
28   * Base class for the remote delegate of AbstractDbOperation.<br>
29   * All other subclasses of AbstractDbOperation should extend AbstractDbOperationRemoteDelegateImpl to reflect
30   * the class hierarchy below AbstractDbOperation.
31   *
32   * @param <P> the database operation class
33   * @author harald
34   */
35  public class AbstractDbOperationRemoteDelegateImpl<P extends AbstractDbOperation<P>>
36         extends RemoteDbDelegateImpl<P>
37         implements AbstractDbOperationRemoteDelegate<P> {
38  
39  
40    /**
41     * operation object associated to the delegate's session.
42     */
43    protected P dbOperation;
44  
45  
46    /**
47     * Creates a delegate on the serverSession socket.
48     *
49     * @param serverSession is the TRIP server session
50     * @param clazz is the subclass of AbstractDbOperation
51     */
52    public AbstractDbOperationRemoteDelegateImpl(RemoteDbSessionImpl serverSession, Class<P> clazz) {
53      super(serverSession, clazz);
54    }
55  
56  
57    /**
58     * Instantiates a singe object and connect it to the db.
59     * <p>
60     * The object is used for all methods not returning a new object and thus
61     * minimizes object construction.
62     */
63    @Override
64    public void initialize() {
65      dbOperation = newInstance();
66    }
67  
68  
69    /**
70     * Creates a new instance of the required class
71     * for methods that return a new object.
72     * @return the new object
73     */
74    protected P newInstance() {
75      try {
76        return getServicedClass().getDeclaredConstructor(Db.class).newInstance(getSession());
77      }
78      catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException |
79             InvocationTargetException e) {
80        throw new PersistenceException(getSession(), "creating object for class '" + getServicedClass() + "' failed", e);
81      }
82    }
83  
84  }