Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializable service factory #168

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions gcloud-java-core/src/main/java/com/google/gcloud/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@

package com.google.gcloud;

public abstract class BaseService<OptionsT extends ServiceOptions<?, OptionsT>>
implements Service<OptionsT> {
public abstract class BaseService<
OptionsT extends ServiceOptions<?, OptionsT>,
ServiceFactoryT extends ServiceFactory<?, OptionsT>>
implements Service<OptionsT, ServiceFactoryT> {

private final OptionsT options;
private final ServiceFactoryT factory;

protected BaseService(OptionsT options) {
protected BaseService(OptionsT options, ServiceFactoryT factory) {
this.options = options;
this.factory = factory;
}

@Override
public OptionsT options() {
return options;
}

@Override
public ServiceFactoryT factory() {
return factory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.google.gcloud;

public interface Service<OptionsT extends ServiceOptions<?, OptionsT>> {
public interface Service<
OptionsT extends ServiceOptions<?, OptionsT>,
ServiceFactoryT extends ServiceFactory<?, OptionsT>> {
OptionsT options();
ServiceFactoryT factory();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.google.gcloud;

import java.io.Serializable;

public interface ServiceFactory<
ServiceT extends Service,
OptionsT extends ServiceOptions<?, OptionsT>>
extends Serializable {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


public ServiceT get(OptionsT options);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
/**
* An interface for Google Cloud Datastore.
*/
public interface Datastore extends Service<DatastoreOptions>, DatastoreReaderWriter {
public interface Datastore extends Service<DatastoreOptions, DatastoreFactory>,
DatastoreReaderWriter {

/**
* Returns a new Datastore transaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@

package com.google.gcloud.datastore;

import com.google.gcloud.ServiceFactory;
import java.io.ObjectStreamException;

/**
* A base class for Datastore factories.
*/
public abstract class DatastoreFactory {
public abstract class DatastoreFactory implements ServiceFactory<Datastore, DatastoreOptions> {

private static final long serialVersionUID = 5037190305022535983L;

private static final DatastoreFactory INSTANCE = new DatastoreFactory() {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@Override
public Datastore get(DatastoreOptions options) {
return new DatastoreImpl(options);
}
};

private static final long serialVersionUID = 5893914895344559491L;

@Override
public Datastore get(DatastoreOptions options) {
return new DatastoreImpl(options, this);
}

private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
};

/**
* Returns the default factory instance.
Expand All @@ -39,5 +50,6 @@ public static DatastoreFactory instance() {
/**
* Returns a {@code Datastore} service for the given options.
*/
@Override
public abstract Datastore get(DatastoreOptions options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import java.util.concurrent.Callable;


final class DatastoreImpl extends BaseService<DatastoreOptions>
final class DatastoreImpl extends BaseService<DatastoreOptions, DatastoreFactory>
implements Datastore {

private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR =
Expand Down Expand Up @@ -72,8 +72,8 @@ public RetryResult beforeEval(Exception exception) {
private final DatastoreRpc datastoreRpc;
private final RetryParams retryParams;

DatastoreImpl(DatastoreOptions options) {
super(options);
DatastoreImpl(DatastoreOptions options, DatastoreFactory factory) {
super(options, factory);
this.datastoreRpc = options.datastoreRpc();
retryParams = MoreObjects.firstNonNull(options.retryParams(), RetryParams.noRetries());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public void testGetOptions() {
assertSame(options, datastore.options());
}

@Test
public void testGetFactory() {
assertSame(DatastoreFactory.instance(), datastore.factory());
}

@Test
public void testNewTransactionCommit() {
Transaction transaction = datastore.newTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

public class SerializationTest {

private static final DatastoreFactory DATASTORE_FACTORY = DatastoreFactory.instance();
private static final IncompleteKey INCOMPLETE_KEY1 =
IncompleteKey.builder("ds", "k").ancestors(PathElement.of("p", 1)).build();
private static final Key KEY1 = Key.builder("ds", "k", "n").build();
Expand Down Expand Up @@ -131,6 +132,13 @@ public class SerializationTest {
.put(ValueType.RAW_VALUE, RAW_VALUE)
.build();

@Test
public void testStorageFactory() throws Exception {
DatastoreFactory serializedCopy = serializeAndDeserialize(DATASTORE_FACTORY);
assertEquals(DATASTORE_FACTORY, serializedCopy);
assertEquals(DATASTORE_FACTORY.hashCode(), serializedCopy.hashCode());
}

@Test
public void testServiceOptions() throws Exception {
DatastoreOptions options = DatastoreOptions.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
* @see <a href="https://cloud.google.com/storage/docs">Google Cloud Storage</a>
*/
public interface Storage extends Service<StorageOptions> {
public interface Storage extends Service<StorageOptions, StorageFactory> {

enum PredefinedAcl {
AUTHENTICATED_READ("authenticatedRead"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@

package com.google.gcloud.storage;

import com.google.gcloud.ServiceFactory;
import java.io.ObjectStreamException;

/**
* A base class for Storage factories.
*/
public abstract class StorageFactory {
public abstract class StorageFactory implements ServiceFactory<Storage, StorageOptions> {

private static final long serialVersionUID = 1866883249985063753L;

private static final StorageFactory INSTANCE = new StorageFactory() {

private static final long serialVersionUID = -7985210081064222485L;

@Override
public Storage get(StorageOptions options) {
return new StorageImpl(options);
return new StorageImpl(options, this);
}

private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
};

Expand All @@ -39,5 +50,6 @@ public static StorageFactory instance() {
/**
* Returns a {@code Storage} service for the given options.
*/
@Override
public abstract Storage get(StorageOptions options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
import java.util.Set;
import java.util.concurrent.Callable;

final class StorageImpl extends BaseService<StorageOptions> implements Storage {
final class StorageImpl extends BaseService<StorageOptions, StorageFactory> implements Storage {

private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {

Expand All @@ -90,8 +90,8 @@ public RetryResult beforeEval(Exception exception) {

private final StorageRpc storageRpc;

StorageImpl(StorageOptions options) {
super(options);
StorageImpl(StorageOptions options, StorageFactory factory) {
super(options, factory);
storageRpc = options.storageRpc();
// todo: configure timeouts - https://developers.google.com/api-client-library/java/google-api-java-client/errors
// todo: provide rewrite - https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

public class SerializationTest {

private static final StorageFactory STORAGE_FACTORY = StorageFactory.instance();
private static final Acl.Domain ACL_DOMAIN = new Acl.Domain("domain");
private static final Acl.Group ACL_GROUP = new Acl.Group("group");
private static final Acl.Project ACL_PROJECT_ = new Acl.Project(ProjectRole.VIEWERS, "pid");
Expand Down Expand Up @@ -65,6 +66,13 @@ public class SerializationTest {
private static final Storage.BucketTargetOption BUCKET_TARGET_OPTIONS =
Storage.BucketTargetOption.metagenerationNotMatch();

@Test
public void testStorageFactory() throws Exception {
StorageFactory serializedCopy = serializeAndDeserialize(STORAGE_FACTORY);
assertEquals(STORAGE_FACTORY, serializedCopy);
assertEquals(STORAGE_FACTORY.hashCode(), serializedCopy.hashCode());
}

@Test
public void testServiceOptions() throws Exception {
StorageOptions options = StorageOptions.builder()
Expand Down