Project

General

Profile

1
/**
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: leinfelder $'
9
 *     '$Date: 2011-09-15 12:57:13 -0700 (Thu, 15 Sep 2011) $'
10
 * '$Revision: 6437 $'
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
package edu.ucsb.nceas.metacat.dataone.hazelcast;
28

    
29
import org.apache.log4j.Logger;
30

    
31
import com.hazelcast.config.Config;
32
import com.hazelcast.config.FileSystemXmlConfig;
33
import com.hazelcast.core.Hazelcast;
34
import com.hazelcast.core.InstanceEvent;
35
import com.hazelcast.core.InstanceListener;
36

    
37
import edu.ucsb.nceas.metacat.properties.PropertyService;
38
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
  private static HazelcastService hzService = null;
51
  
52
  /* The Hazelcast configuration */
53
  private Config hzConfig;
54
  
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
      init();
66
      
67
    } catch (ServiceException se) {
68
      logMetacat.error("There was a problem creating the HazelcastService. " +
69
                       "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
    if ( hzService == null ) {
84
      
85
      hzService = new HazelcastService();
86
      
87
    }
88
    return hzService;
89
  }
90
  
91
  /**
92
   * Initializes the Hazelcast service
93
   */
94
  public void init() throws ServiceException {
95
    
96
    logMetacat.debug("HazelcastService.doRefresh() called.");
97
    
98
    try {
99
    	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
      logMetacat.error(msg);
106
      throw new ServiceException(msg);
107
    
108
    }
109
    
110
    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
    // TODO: Determine the consequences of restarting the Hazelcast instance
121
    // 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
    
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
  @Override
141
  public void instanceCreated(InstanceEvent event) {
142
    logMetacat.info("New Hazelcast instance created: " +
143
      event.getInstance().getId() + ", " +
144
      event.getInstance().getInstanceType());
145
    
146
  }
147

    
148
  @Override
149
  public void instanceDestroyed(InstanceEvent event) {
150
    logMetacat.info("Hazelcast instance removed: " +
151
        event.getInstance().getId() + ", " +
152
        event.getInstance().getInstanceType());
153
    
154
  }
155
  
156
  /**
157
   * Refresh the Hazelcast service by restarting it
158
   */
159
  @Override
160
  protected void doRefresh() throws ServiceException {
161

    
162
    // TODO: verify that the correct config file is still used
163
    Hazelcast.getLifecycleService().restart();
164
    
165
  }
166

    
167
}
(1-1/4)