Java tutorial
/** Copyright 2014 OpenSearchServer, Inc. * http://www.opensearchserver.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ package com.opensearchserver.hadse.index.shard; import java.io.Closeable; import java.io.File; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.opensearchserver.hadse.Hadse; import com.opensearchserver.hadse.cluster.ClusterCatalog; import com.opensearchserver.hadse.utils.ReadWriteLock; @JsonInclude(Include.NON_EMPTY) public abstract class ShardItem implements Closeable { /** * The location of the directory or file */ protected final ShardDef shardDef; private Status status = Status.OFFLINE; private String error = null; protected ShardItem(ShardDef shardDef) { this.shardDef = shardDef; } final public String getName() { return shardDef.getName(); } final public Status getStatus() { statusLock.r.lock(); try { return status; } finally { statusLock.r.unlock(); } } public String getAddress() { return shardDef.getAddress(); } final public String getError() { statusLock.r.lock(); try { return error; } finally { statusLock.r.unlock(); } } private final ReadWriteLock statusLock = new ReadWriteLock(); final protected void setStatus(Status status, String error) { statusLock.w.lock(); try { this.status = status; this.error = error; } finally { statusLock.w.unlock(); } if (error != null) Hadse.LOG.error(error); } public static ShardItem load(ShardDef shardDef, File indexDirectory) { if (ClusterCatalog.isLocal(shardDef.getAddress())) return new LocalShardItem(shardDef, indexDirectory); // TODO RemoteShardItem return null; } public enum Status { ONLINE, OFFLINE, ERROR; } }