blob: f0f5a706497d5f9672738b2232e113bf942d2f79 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.ignite.internal;
import org.apache.ignite.*;
import org.apache.ignite.cluster.*;
import org.apache.ignite.internal.cluster.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.services.*;
import org.jetbrains.annotations.*;
import java.io.*;
import java.util.*;
/**
* {@link org.apache.ignite.IgniteCompute} implementation.
*/
public class IgniteServicesImpl extends AsyncSupportAdapter implements IgniteServices, Externalizable {
/** */
private static final long serialVersionUID = 0L;
/** */
private GridKernalContext ctx;
/** */
private ClusterGroupAdapter prj;
/**
* Required by {@link Externalizable}.
*/
public IgniteServicesImpl() {
// No-op.
}
/**
* @param ctx Kernal context.
* @param prj Projection.
* @param async Async support flag.
*/
public IgniteServicesImpl(GridKernalContext ctx, ClusterGroupAdapter prj, boolean async) {
super(async);
this.ctx = ctx;
this.prj = prj;
}
/** {@inheritDoc} */
@Override public ClusterGroup clusterGroup() {
return prj;
}
/** {@inheritDoc} */
@Override public void deployNodeSingleton(String name, Service svc) {
A.notNull(name, "name");
A.notNull(svc, "svc");
guard();
try {
saveOrGet(ctx.service().deployNodeSingleton(prj, name, svc));
}
catch (IgniteCheckedException e) {
throw U.convertException(e);
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public void deployClusterSingleton(String name, Service svc) {
A.notNull(name, "name");
A.notNull(svc, "svc");
guard();
try {
saveOrGet(ctx.service().deployClusterSingleton(prj, name, svc));
}
catch (IgniteCheckedException e) {
throw U.convertException(e);
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public void deployMultiple(String name, Service svc, int totalCnt, int maxPerNodeCnt) {
A.notNull(name, "name");
A.notNull(svc, "svc");
guard();
try {
saveOrGet(ctx.service().deployMultiple(prj, name, svc, totalCnt, maxPerNodeCnt));
}
catch (IgniteCheckedException e) {
throw U.convertException(e);
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public void deployKeyAffinitySingleton(String name, Service svc, @Nullable String cacheName,
Object affKey) {
A.notNull(name, "name");
A.notNull(svc, "svc");
A.notNull(affKey, "affKey");
guard();
try {
saveOrGet(ctx.service().deployKeyAffinitySingleton(name, svc, cacheName, affKey));
}
catch (IgniteCheckedException e) {
throw U.convertException(e);
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public void deploy(ServiceConfiguration cfg) {
A.notNull(cfg, "cfg");
guard();
try {
saveOrGet(ctx.service().deploy(cfg));
}
catch (IgniteCheckedException e) {
throw U.convertException(e);
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public void cancel(String name) {
A.notNull(name, "name");
guard();
try {
saveOrGet(ctx.service().cancel(name));
}
catch (IgniteCheckedException e) {
throw U.convertException(e);
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public void cancelAll() {
guard();
try {
saveOrGet(ctx.service().cancelAll());
}
catch (IgniteCheckedException e) {
throw U.convertException(e);
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public Collection<ServiceDescriptor> serviceDescriptors() {
guard();
try {
return ctx.service().serviceDescriptors();
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public <T> T service(String name) {
guard();
try {
return ctx.service().service(name);
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public <T> T serviceProxy(String name, Class<? super T> svcItf, boolean sticky)
throws IgniteException {
A.notNull(name, "name");
A.notNull(svcItf, "svcItf");
A.ensure(svcItf.isInterface(), "Service class must be an interface: " + svcItf);
guard();
try {
return ctx.service().serviceProxy(prj, name, svcItf, sticky);
}
finally {
unguard();
}
}
/** {@inheritDoc} */
@Override public <T> Collection<T> services(String name) {
guard();
try {
return ctx.service().services(name);
}
finally {
unguard();
}
}
/**
* <tt>ctx.gateway().readLock()</tt>
*/
private void guard() {
ctx.gateway().readLock();
}
/**
* <tt>ctx.gateway().readUnlock()</tt>
*/
private void unguard() {
ctx.gateway().readUnlock();
}
/** {@inheritDoc} */
@Override public IgniteServices withAsync() {
if (isAsync())
return this;
return new IgniteServicesImpl(ctx, prj, true);
}
/** {@inheritDoc} */
@Override public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(prj);
}
/** {@inheritDoc} */
@Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
prj = (ClusterGroupAdapter)in.readObject();
}
/**
* Reconstructs object on unmarshalling.
*
* @return Reconstructed object.
* @throws ObjectStreamException Thrown in case of unmarshalling error.
*/
protected Object readResolve() throws ObjectStreamException {
return prj.services();
}
}