Project

General

Profile

1 6398 cjones
/**
2
 *  '$RCSfile$'
3
 *    Purpose: Implements a service for managing a Hazelcast cluster member
4
 *  Copyright: 2011 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Christopher Jones
7
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27 6399 leinfelder
package edu.ucsb.nceas.metacat.dataone.hazelcast;
28 6398 cjones
29
import org.apache.log4j.Logger;
30
31 6401 cjones
import com.hazelcast.config.Config;
32 6437 leinfelder
import com.hazelcast.config.FileSystemXmlConfig;
33 6398 cjones
import com.hazelcast.core.Hazelcast;
34
import com.hazelcast.core.InstanceEvent;
35
import com.hazelcast.core.InstanceListener;
36
37 6401 cjones
import edu.ucsb.nceas.metacat.properties.PropertyService;
38 6398 cjones
import edu.ucsb.nceas.metacat.shared.BaseService;
39
import edu.ucsb.nceas.metacat.shared.ServiceException;
40
/**
41
 * The Hazelcast service enables Metacat as a Hazelcast cluster member
42
 */
43
public class HazelcastService extends BaseService
44
  implements InstanceListener {
45
46
  /* The instance of the logging class */
47
  private static Logger logMetacat = Logger.getLogger(HazelcastService.class);
48
49
  /* The singleton instance of the hazelcast service */
50 6401 cjones
  private static HazelcastService hzService = null;
51 6398 cjones
52 6401 cjones
  /* The Hazelcast configuration */
53
  private Config hzConfig;
54 6398 cjones
55
  /*
56
   * Constructor: Creates an instance of the hazelcast service. Since
57
   * this uses a singleton pattern, use getInstance() to gain the instance.
58
   */
59
  private HazelcastService() {
60
61
    super();
62
    _serviceName="HazelcastService";
63
64
    try {
65 6401 cjones
      init();
66 6398 cjones
67
    } catch (ServiceException se) {
68 6437 leinfelder
      logMetacat.error("There was a problem creating the HazelcastService. " +
69 6398 cjones
                       "The error message was: " + se.getMessage());
70
71
    }
72
73
  }
74
75
  /**
76
   *  Get the instance of the HazelcastService that has been instantiated,
77
   *  or instantiate one if it has not been already.
78
   *
79
   * @return hazelcastService - The instance of the hazelcast service
80
   */
81
  public static HazelcastService getInstance(){
82
83 6401 cjones
    if ( hzService == null ) {
84 6398 cjones
85 6401 cjones
      hzService = new HazelcastService();
86 6398 cjones
87
    }
88 6401 cjones
    return hzService;
89 6398 cjones
  }
90
91
  /**
92 6401 cjones
   * Initializes the Hazelcast service
93 6398 cjones
   */
94 6401 cjones
  public void init() throws ServiceException {
95 6398 cjones
96
    logMetacat.debug("HazelcastService.doRefresh() called.");
97
98 6401 cjones
    try {
99 6437 leinfelder
    	String configFileName = PropertyService.getProperty("dataone.hazelcast.configFilePath");
100
//    	System.setProperty("hazelcast.config", configFileName);
101
		Config config = new FileSystemXmlConfig(configFileName);
102
		Hazelcast.init(config);
103
    } catch (Exception e) {
104
      String msg = e.getMessage();
105 6407 cjones
      logMetacat.error(msg);
106
      throw new ServiceException(msg);
107 6398 cjones
108 6401 cjones
    }
109
110 6398 cjones
    return;
111
112
  }
113
114
  /**
115
   * Indicate whether or not this service is refreshable.
116
   *
117
   * @return refreshable - the boolean refreshable status
118
   */
119
  public boolean refreshable() {
120 6401 cjones
    // TODO: Determine the consequences of restarting the Hazelcast instance
121 6407 cjones
    // Set this to true if it's okay to drop from the cluster, lose the maps,
122
    // and start back up again
123
    return false;
124 6398 cjones
125
  }
126
127
  /**
128
   * Stop the HazelcastService. When stopped, the service will no longer
129
   * respond to requests.
130
   */
131
  public void stop() throws ServiceException {
132
133
    Hazelcast.getLifecycleService().shutdown();
134
135
  }
136
137
  /**
138
   * Listen for new Hazelcast member events
139
   */
140 6407 cjones
  @Override
141 6398 cjones
  public void instanceCreated(InstanceEvent event) {
142 6407 cjones
    logMetacat.info("New Hazelcast instance created: " +
143
      event.getInstance().getId() + ", " +
144
      event.getInstance().getInstanceType());
145
146 6398 cjones
  }
147
148 6407 cjones
  @Override
149 6398 cjones
  public void instanceDestroyed(InstanceEvent event) {
150 6407 cjones
    logMetacat.info("Hazelcast instance removed: " +
151
        event.getInstance().getId() + ", " +
152
        event.getInstance().getInstanceType());
153
154 6398 cjones
  }
155 6407 cjones
156
  /**
157
   * Refresh the Hazelcast service by restarting it
158
   */
159
  @Override
160 6401 cjones
  protected void doRefresh() throws ServiceException {
161
162 6407 cjones
    // TODO: verify that the correct config file is still used
163 6401 cjones
    Hazelcast.getLifecycleService().restart();
164 6407 cjones
165 6401 cjones
  }
166
167 6398 cjones
}