Review Board 1.7.22


SQOOP-802 Sqoop2: Convert main "Manager" classes to singletons.

Review Request #8753 - Created Dec. 23, 2012 and submitted

Jarek Cecho
SQOOP-802
Reviewers
Sqoop
sqoop-sqoop2
I've changed the implementation as proposed on JIRA. No new or changed functionality was introduced.
Unit tests are passing and I've tested this on real cluster.
core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
Revision c2342f727f58e56f9eaad26f861ece7b7cea858d New Change
[20] 37 lines
[+20]
38
import org.apache.sqoop.repository.RepositoryTransaction;
38
import org.apache.sqoop.repository.RepositoryTransaction;
39
import org.apache.sqoop.model.MConnector;
39
import org.apache.sqoop.model.MConnector;
40

    
   
40

   
41
public final class ConnectorManager {
41
public final class ConnectorManager {
42

    
   
42

   

    
   
43
  /**

    
   
44
   * Logger object.

    
   
45
   */
43
  private static final Logger LOG = Logger.getLogger(ConnectorManager.class);
46
  private static final Logger LOG = Logger.getLogger(ConnectorManager.class);
44

    
   
47

   

    
   
48
  /**

    
   
49
   * Private instance to singleton of this class.

    
   
50
   */

    
   
51
  private static ConnectorManager instance;

    
   
52

   

    
   
53
  /**

    
   
54
   * Create default object by default.

    
   
55
   *

    
   
56
   * Every Sqoop server application needs one so this should not be performance issue.

    
   
57
   */

    
   
58
  static {

    
   
59
    instance = new ConnectorManager();

    
   
60
  }

    
   
61

   

    
   
62
  /**

    
   
63
   * Return current instance.

    
   
64
   *

    
   
65
   * @return Current instance

    
   
66
   */

    
   
67
  public static ConnectorManager getInstance() {

    
   
68
    return instance;

    
   
69
  }

    
   
70

   

    
   
71
  /**

    
   
72
   * Allows to set instance in case that it's need.

    
   
73
   *

    
   
74
   * This method should not be normally used as the default instance should be sufficient. One target

    
   
75
   * user use case for this method are unit tests.

    
   
76
   *

    
   
77
   * @param newInstance New instance

    
   
78
   */

    
   
79
  public static void setInstance(ConnectorManager newInstance) {

    
   
80
    instance = newInstance;

    
   
81
  }

    
   
82

   
45
  // key: connector id, value: connector name
83
  // key: connector id, value: connector name
46
  private static Map<Long, String> nameMap = new HashMap<Long, String>();
84
  private Map<Long, String> nameMap = new HashMap<Long, String>();
47

    
   
85

   
48
  // key: connector name, value: connector handler
86
  // key: connector name, value: connector handler
49
  private static Map<String, ConnectorHandler> handlerMap =
87
  private Map<String, ConnectorHandler> handlerMap =
50
      new HashMap<String, ConnectorHandler>();
88
      new HashMap<String, ConnectorHandler>();
51

    
   
89

   
52
  public static List<MConnector> getConnectorsMetadata() {
90
  public List<MConnector> getConnectorsMetadata() {
53
    List<MConnector> connectors = new LinkedList<MConnector>();
91
    List<MConnector> connectors = new LinkedList<MConnector>();
54
    for(ConnectorHandler handler : handlerMap.values()) {
92
    for(ConnectorHandler handler : handlerMap.values()) {
55
      connectors.add(handler.getMetadata());
93
      connectors.add(handler.getMetadata());
56
    }
94
    }
57
    return connectors;
95
    return connectors;
58
  }
96
  }
59

    
   
97

   
60
  public static Set<Long> getConnectorIds() {
98
  public Set<Long> getConnectorIds() {
61
    return nameMap.keySet();
99
    return nameMap.keySet();
62
  }
100
  }
63

    
   
101

   
64
  public static Map<Long, ResourceBundle> getResourceBundles(Locale locale) {
102
  public Map<Long, ResourceBundle> getResourceBundles(Locale locale) {
65
    Map<Long, ResourceBundle> bundles = new HashMap<Long, ResourceBundle>();
103
    Map<Long, ResourceBundle> bundles = new HashMap<Long, ResourceBundle>();
66
    for(ConnectorHandler handler : handlerMap.values()) {
104
    for(ConnectorHandler handler : handlerMap.values()) {
67
      long id = handler.getMetadata().getPersistenceId();
105
      long id = handler.getMetadata().getPersistenceId();
68
      ResourceBundle bundle = handler.getConnector().getBundle(locale);
106
      ResourceBundle bundle = handler.getConnector().getBundle(locale);
69
      bundles.put(id, bundle);
107
      bundles.put(id, bundle);
70
    }
108
    }
71
    return bundles;
109
    return bundles;
72
  }
110
  }
73

    
   
111

   
74
  public static ResourceBundle getResourceBundle(long connectorId,
112
  public ResourceBundle getResourceBundle(long connectorId,
75
                                                 Locale locale) {
113
                                                 Locale locale) {
76
    ConnectorHandler handler = handlerMap.get(nameMap.get(connectorId));
114
    ConnectorHandler handler = handlerMap.get(nameMap.get(connectorId));
77
    return  handler.getConnector().getBundle(locale);
115
    return  handler.getConnector().getBundle(locale);
78
  }
116
  }
79

    
   
117

   
80
  public static MConnector getConnectorMetadata(long connectorId) {
118
  public MConnector getConnectorMetadata(long connectorId) {
81
    ConnectorHandler handler = handlerMap.get(nameMap.get(connectorId));
119
    ConnectorHandler handler = handlerMap.get(nameMap.get(connectorId));
82
    if(handler == null) {
120
    if(handler == null) {
83
      return null;
121
      return null;
84
    }
122
    }
85

    
   
123

   
86
    return handler.getMetadata();
124
    return handler.getMetadata();
87
  }
125
  }
88

    
   
126

   
89
  public static SqoopConnector getConnector(long connectorId) {
127
  public SqoopConnector getConnector(long connectorId) {
90
    ConnectorHandler handler = handlerMap.get(nameMap.get(connectorId));
128
    ConnectorHandler handler = handlerMap.get(nameMap.get(connectorId));
91
    return handler.getConnector();
129
    return handler.getConnector();
92
  }
130
  }
93

    
   
131

   
94
  public static synchronized void initialize() {
132
  public synchronized void initialize() {
95
    if (LOG.isTraceEnabled()) {
133
    if (LOG.isTraceEnabled()) {
96
      LOG.trace("Begin connector manager initialization");
134
      LOG.trace("Begin connector manager initialization");
97
    }
135
    }
98

    
   
136

   
99
    List<URL> connectorConfigs = new ArrayList<URL>();
137
    List<URL> connectorConfigs = new ArrayList<URL>();
[+20] [20] 45 lines
[+20] public static synchronized void initialize() { [+] public synchronized void initialize() {
145
    if (LOG.isInfoEnabled()) {
183
    if (LOG.isInfoEnabled()) {
146
      LOG.info("Connectors loaded: " + handlerMap);
184
      LOG.info("Connectors loaded: " + handlerMap);
147
    }
185
    }
148
  }
186
  }
149

    
   
187

   
150
  private static synchronized void registerConnectors() {
188
  private synchronized void registerConnectors() {
151
    Repository repository = RepositoryManager.getRepository();
189
    Repository repository = RepositoryManager.getInstance().getRepository();
152

    
   
190

   
153
    RepositoryTransaction rtx = null;
191
    RepositoryTransaction rtx = null;
154
    try {
192
    try {
155
      rtx = repository.getTransaction();
193
      rtx = repository.getTransaction();
156
      rtx.begin();
194
      rtx.begin();
[+20] [20] 26 lines
[+20] private static synchronized void registerConnectors() { [+] private synchronized void registerConnectors() {
183
        rtx.close();
221
        rtx.close();
184
      }
222
      }
185
    }
223
    }
186
  }
224
  }
187

    
   
225

   
188
  public static synchronized void destroy() {
226
  public synchronized void destroy() {
189
    // FIXME
227
    // FIXME
190
  }
228
  }
191

    
   

   
192
  private ConnectorManager() {

   
193
    // Instantiation of this class is prohibited

   
194
  }

   
195
}
229
}
core/src/main/java/org/apache/sqoop/core/SqoopConfiguration.java
Revision 043f8e6a18a6e584be9dc4454c115724ae7dbdd5 New Change
 
core/src/main/java/org/apache/sqoop/framework/FrameworkManager.java
Revision a5ac74f8fe183a387b383b95ece7424cdd983237 New Change
 
core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryProvider.java
Revision eb7ed5b7ea89da2c7a775fb4290cfd5fca4bf2ce New Change
 
core/src/main/java/org/apache/sqoop/repository/RepositoryManager.java
Revision 632bc603d7a34dc9d03f093014b826715d755cdb New Change
 
core/src/test/java/org/apache/sqoop/core/TestConfiguration.java
Revision bd912eabcad844203fea07eeb77cece0988835bd New Change
 
core/src/test/java/org/apache/sqoop/repository/TestRepositoryManager.java
Revision a95c2855e05c99f93a6042b3d470844291d8a253 New Change
 
server/src/main/java/org/apache/sqoop/handler/ConnectionRequestHandler.java
Revision b77c5d76974cdf791ed414041a39bbd79760ceec New Change
 
server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java
Revision 6dee70ce589f4ba18fd16047245f496ed718bc0f New Change
 
server/src/main/java/org/apache/sqoop/handler/FrameworkRequestHandler.java
Revision 45a2d42d86658510db4e17c5a913aec1e14625e9 New Change
 
server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java
Revision 6dea45f02a598558862014f4a271ddd5df8656fe New Change
 
server/src/main/java/org/apache/sqoop/handler/SubmissionRequestHandler.java
Revision 6e541d501aa5ead353a6ec6e284e932669f6c60a New Change
 
server/src/main/java/org/apache/sqoop/server/ServerInitializer.java
Revision 256262bb01b1e1ceb64f1ed598cc77302866319e New Change
 
  1. core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java: Loading...
  2. core/src/main/java/org/apache/sqoop/core/SqoopConfiguration.java: Loading...
  3. core/src/main/java/org/apache/sqoop/framework/FrameworkManager.java: Loading...
  4. core/src/main/java/org/apache/sqoop/repository/JdbcRepositoryProvider.java: Loading...
  5. core/src/main/java/org/apache/sqoop/repository/RepositoryManager.java: Loading...
  6. core/src/test/java/org/apache/sqoop/core/TestConfiguration.java: Loading...
  7. core/src/test/java/org/apache/sqoop/repository/TestRepositoryManager.java: Loading...
  8. server/src/main/java/org/apache/sqoop/handler/ConnectionRequestHandler.java: Loading...
  9. server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java: Loading...
  10. server/src/main/java/org/apache/sqoop/handler/FrameworkRequestHandler.java: Loading...
  11. server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java: Loading...
  12. server/src/main/java/org/apache/sqoop/handler/SubmissionRequestHandler.java: Loading...
  13. server/src/main/java/org/apache/sqoop/server/ServerInitializer.java: Loading...