Skip to content

Commit 701b3d2

Browse files
committed
---
yaml --- r: 913 b: refs/heads/master c: a6a2898 h: refs/heads/master i: 911: 6836922 v: v3
1 parent 8d27127 commit 701b3d2

12 files changed

Lines changed: 1381 additions & 61 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2-
refs/heads/master: da3c441dc280fea1a544ccda2859f90a5b0bad72
2+
refs/heads/master: a6a2898cdc98289808b6e10dec28204775150e37
33
refs/heads/travis: 0fa997e2fc9c6b61b2d91e6d163655aae67d44b6
44
refs/heads/gh-pages: 5a10432ecc75f29812e33a8236c900379509fe99

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
public class DatastoreOptionsTest {
3535

3636
private static final String PROJECT_ID = "project_id";
37+
private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
3738
private DatastoreRpcFactory datastoreRpcFactory;
3839
private DatastoreRpc datastoreRpc;
3940
private DatastoreOptions.Builder options;
@@ -46,7 +47,7 @@ public void setUp() throws IOException, InterruptedException {
4647
.normalizeDataset(false)
4748
.serviceRpcFactory(datastoreRpcFactory)
4849
.projectId(PROJECT_ID)
49-
.host("http://localhost:" + LocalGcdHelper.PORT);
50+
.host("http://localhost:" + PORT);
5051
EasyMock.expect(datastoreRpcFactory.create(EasyMock.anyObject(DatastoreOptions.class)))
5152
.andReturn(datastoreRpc)
5253
.anyTimes();
@@ -60,7 +61,7 @@ public void testProjectId() throws Exception {
6061

6162
@Test
6263
public void testHost() throws Exception {
63-
assertEquals("http://localhost:" + LocalGcdHelper.PORT, options.build().host());
64+
assertEquals("http://localhost:" + PORT, options.build().host());
6465
}
6566

6667
@Test

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,23 @@ public class DatastoreTest {
103103
private Datastore datastore;
104104

105105
private static LocalGcdHelper gcdHelper;
106+
private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
106107

107108
@Rule
108109
public ExpectedException thrown = ExpectedException.none();
109110

110111
@BeforeClass
111112
public static void beforeClass() throws IOException, InterruptedException {
112-
if (!LocalGcdHelper.isActive(PROJECT_ID)) {
113-
gcdHelper = LocalGcdHelper.start(PROJECT_ID);
113+
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
114+
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT);
114115
}
115116
}
116117

117118
@Before
118119
public void setUp() throws IOException, InterruptedException {
119120
options = DatastoreOptions.builder()
120121
.projectId(PROJECT_ID)
121-
.host("http://localhost:" + LocalGcdHelper.PORT)
122+
.host("http://localhost:" + PORT)
122123
.build();
123124
datastore = DatastoreFactory.instance().get(options);
124125
StructuredQuery<Key> query = Query.keyQueryBuilder().build();

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java

Lines changed: 77 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.math.BigInteger;
3636
import java.net.HttpURLConnection;
3737
import java.net.MalformedURLException;
38+
import java.net.ServerSocket;
3839
import java.net.URL;
3940
import java.nio.channels.Channels;
4041
import java.nio.channels.ReadableByteChannel;
@@ -49,8 +50,10 @@
4950
import java.security.NoSuchAlgorithmException;
5051
import java.util.ArrayList;
5152
import java.util.Arrays;
53+
import java.util.HashMap;
5254
import java.util.List;
5355
import java.util.Locale;
56+
import java.util.Map;
5457
import java.util.regex.Pattern;
5558
import java.util.zip.ZipEntry;
5659
import java.util.zip.ZipInputStream;
@@ -68,9 +71,10 @@ public class LocalGcdHelper {
6871
private final String projectId;
6972
private Path gcdPath;
7073
private ProcessStreamReader processReader;
74+
private final int port;
7175

7276
public static final String DEFAULT_PROJECT_ID = "projectid1";
73-
public static final int PORT = 8080;
77+
public static final int DEFAULT_PORT = 8080;
7478
private static final String GCD_VERSION = "v1beta2";
7579
private static final String GCD_BUILD = "rev1-2.1.2b";
7680
private static final String GCD_BASENAME = "gcd-" + GCD_VERSION + "-" + GCD_BUILD;
@@ -94,6 +98,14 @@ public class LocalGcdHelper {
9498
}
9599
}
96100

101+
public static int findAvailablePort(int defaultPort) {
102+
try (ServerSocket tempSocket = new ServerSocket(0)) {
103+
return tempSocket.getLocalPort();
104+
} catch (IOException e) {
105+
return defaultPort;
106+
}
107+
}
108+
97109
private static Path installedGcdPath() {
98110
String gcloudExecutableName;
99111
if (isWindows()) {
@@ -283,8 +295,9 @@ public static CommandWrapper create() {
283295
}
284296
}
285297

286-
public LocalGcdHelper(String projectId) {
298+
public LocalGcdHelper(String projectId, int port) {
287299
this.projectId = projectId;
300+
this.port = port;
288301
}
289302

290303
/**
@@ -297,7 +310,7 @@ public LocalGcdHelper(String projectId) {
297310
*/
298311
public void start() throws IOException, InterruptedException {
299312
// send a quick request in case we have a hanging process from a previous run
300-
sendQuitRequest();
313+
sendQuitRequest(port);
301314
// Each run is associated with its own folder that is deleted once test completes.
302315
gcdPath = Files.createTempDirectory("gcd");
303316
File gcdFolder = gcdPath.toFile();
@@ -379,13 +392,12 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
379392
if (log.isLoggable(Level.FINE)) {
380393
log.log(Level.FINE, "Starting datastore emulator for the project: {0}", projectId);
381394
}
382-
Process startProcess =
383-
CommandWrapper.create()
384-
.command(gcdAbsolutePath.toString(), "start", "--testing", "--allow_remote_shutdown",
385-
projectId)
386-
.directory(gcdPath)
387-
.redirectErrorStream()
388-
.start();
395+
Process startProcess = CommandWrapper.create()
396+
.command(gcdAbsolutePath.toString(), "start", "--testing", "--allow_remote_shutdown",
397+
"--port=" + Integer.toString(port), projectId)
398+
.directory(gcdPath)
399+
.redirectErrorStream()
400+
.start();
389401
processReader = ProcessStreamReader.start(startProcess, "Dev App Server is now running");
390402
}
391403

@@ -419,9 +431,9 @@ private static void extractFile(ZipInputStream zipIn, File filePath) throws IOEx
419431
}
420432
}
421433

422-
public static void sendQuitRequest() {
434+
public static void sendQuitRequest(int port) {
423435
try {
424-
URL url = new URL("http", "localhost", PORT, "/_ah/admin/quit");
436+
URL url = new URL("http", "localhost", port, "/_ah/admin/quit");
425437
HttpURLConnection con = (HttpURLConnection) url.openConnection();
426438
con.setRequestMethod("POST");
427439
con.setDoOutput(true);
@@ -439,7 +451,7 @@ public static void sendQuitRequest() {
439451
}
440452

441453
public void stop() throws IOException, InterruptedException {
442-
sendQuitRequest();
454+
sendQuitRequest(port);
443455
if (processReader != null) {
444456
processReader.terminate();
445457
}
@@ -468,44 +480,70 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
468480
});
469481
}
470482

471-
public static LocalGcdHelper start(String projectId) throws IOException, InterruptedException {
472-
LocalGcdHelper helper = new LocalGcdHelper(projectId);
483+
public static LocalGcdHelper start(String projectId, int port)
484+
throws IOException, InterruptedException {
485+
LocalGcdHelper helper = new LocalGcdHelper(projectId, port);
473486
helper.start();
474487
return helper;
475488
}
476489

477490
public static void main(String... args) throws IOException, InterruptedException {
478-
if (args.length == 1) {
479-
switch (args[0]) {
480-
case "START":
481-
if (!isActive(DEFAULT_PROJECT_ID)) {
482-
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID);
483-
try (FileWriter writer = new FileWriter(".local_gcd_helper")) {
484-
writer.write(helper.gcdPath.toAbsolutePath().toString());
485-
}
491+
Map<String, String> parsedArgs = parseArgs(args);
492+
String action = parsedArgs.get("action");
493+
int port = (parsedArgs.get("port") == null) ? DEFAULT_PORT
494+
: Integer.parseInt(parsedArgs.get("port"));
495+
switch (action) {
496+
case "START":
497+
if (!isActive(DEFAULT_PROJECT_ID, port)) {
498+
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID, port);
499+
try (FileWriter writer = new FileWriter(".local_gcd_helper")) {
500+
writer.write(
501+
helper.gcdPath.toAbsolutePath().toString() + System.lineSeparator());
502+
writer.write(Integer.toString(port));
486503
}
487-
return;
488-
case "STOP":
489-
sendQuitRequest();
490-
File file = new File(".local_gcd_helper");
491-
if (file.exists()) {
492-
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
493-
String path = reader.readLine();
494-
deleteRecurse(Paths.get(path));
495-
}
496-
file.delete();
504+
}
505+
return;
506+
case "STOP":
507+
File file = new File(".local_gcd_helper");
508+
String path = null;
509+
boolean fileExists = file.exists();
510+
if (fileExists) {
511+
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
512+
path = reader.readLine();
513+
port = Integer.parseInt(reader.readLine());
497514
}
498-
return;
499-
default:
500-
break;
515+
}
516+
sendQuitRequest(port);
517+
if (fileExists) {
518+
deleteRecurse(Paths.get(path));
519+
file.delete();
520+
}
521+
return;
522+
default:
523+
break;
524+
}
525+
}
526+
527+
private static Map<String, String> parseArgs(String[] args) {
528+
Map<String, String> parsedArgs = new HashMap<String, String>();
529+
for (String arg : args) {
530+
if (arg.startsWith("--port=")) {
531+
parsedArgs.put("port", arg.substring("--port=".length()));
532+
} else if (arg.equals("START") || arg.equals("STOP")) {
533+
parsedArgs.put("action", arg);
534+
} else {
535+
throw new RuntimeException("Only accepts START, STOP, and --port=<port #> as arguments");
501536
}
502537
}
503-
throw new RuntimeException("expecting only START | STOP");
538+
if (parsedArgs.get("action") == null) {
539+
throw new RuntimeException("EXPECTING START | STOP");
540+
}
541+
return parsedArgs;
504542
}
505543

506-
public static boolean isActive(String projectId) {
544+
public static boolean isActive(String projectId, int port) {
507545
try {
508-
StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(PORT);
546+
StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(port);
509547
urlBuilder.append("/datastore/v1beta2/datasets/").append(projectId).append("/lookup");
510548
URL url = new URL(urlBuilder.toString());
511549
try (BufferedReader reader =

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.google.api.client.http.HttpResponse;
4444
import com.google.api.client.http.HttpResponseException;
4545
import com.google.api.client.http.HttpTransport;
46+
import com.google.api.client.http.InputStreamContent;
4647
import com.google.api.client.http.json.JsonHttpContent;
4748
import com.google.api.client.json.JsonFactory;
4849
import com.google.api.client.json.jackson.JacksonFactory;
@@ -63,6 +64,7 @@
6364

6465
import java.io.ByteArrayOutputStream;
6566
import java.io.IOException;
67+
import java.io.InputStream;
6668
import java.util.ArrayList;
6769
import java.util.List;
6870
import java.util.Map;
@@ -119,13 +121,14 @@ public Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageExcept
119121
}
120122

121123
@Override
122-
public StorageObject create(StorageObject storageObject, final byte[] content,
124+
public StorageObject create(StorageObject storageObject, final InputStream content,
123125
Map<Option, ?> options) throws StorageException {
124126
try {
125-
return storage.objects()
127+
Storage.Objects.Insert insert = storage.objects()
126128
.insert(storageObject.getBucket(), storageObject,
127-
new ByteArrayContent(storageObject.getContentType(), content))
128-
.setProjection(DEFAULT_PROJECTION)
129+
new InputStreamContent(storageObject.getContentType(), content));
130+
insert.getMediaHttpUploader().setDirectUploadEnabled(true);
131+
return insert.setProjection(DEFAULT_PROJECTION)
129132
.setPredefinedAcl(PREDEFINED_ACL.getString(options))
130133
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
131134
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
@@ -328,10 +331,11 @@ public StorageObject copy(StorageObject source, Map<Option, ?> sourceOptions,
328331
.copy(source.getBucket(), source.getName(), target.getBucket(), target.getName(),
329332
target.getContentType() != null ? target : null)
330333
.setProjection(DEFAULT_PROJECTION)
331-
.setIfMetagenerationMatch(IF_SOURCE_METAGENERATION_MATCH.getLong(sourceOptions))
332-
.setIfMetagenerationNotMatch(IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(sourceOptions))
333-
.setIfGenerationMatch(IF_SOURCE_GENERATION_MATCH.getLong(sourceOptions))
334-
.setIfGenerationNotMatch(IF_SOURCE_GENERATION_NOT_MATCH.getLong(sourceOptions))
334+
.setIfSourceMetagenerationMatch(IF_SOURCE_METAGENERATION_MATCH.getLong(sourceOptions))
335+
.setIfSourceMetagenerationNotMatch(
336+
IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(sourceOptions))
337+
.setIfSourceGenerationMatch(IF_SOURCE_GENERATION_MATCH.getLong(sourceOptions))
338+
.setIfSourceGenerationNotMatch(IF_SOURCE_GENERATION_NOT_MATCH.getLong(sourceOptions))
335339
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(targetOptions))
336340
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(targetOptions))
337341
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(targetOptions))
@@ -521,4 +525,3 @@ public String open(StorageObject object, Map<Option, ?> options)
521525
}
522526
}
523527
}
524-

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.collect.ImmutableMap;
2323
import com.google.gcloud.storage.StorageException;
2424

25+
import java.io.InputStream;
2526
import java.util.List;
2627
import java.util.Map;
2728

@@ -128,7 +129,7 @@ public BatchResponse(Map<StorageObject, Tuple<Boolean, StorageException>> delete
128129

129130
Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageException;
130131

131-
StorageObject create(StorageObject object, byte[] content, Map<Option, ?> options)
132+
StorageObject create(StorageObject object, InputStream content, Map<Option, ?> options)
132133
throws StorageException;
133134

134135
Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) throws StorageException;

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java renamed to trunk/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* Default implementation for BlobWriteChannel.
3434
*/
35-
class BlobWriterChannelImpl implements BlobWriteChannel {
35+
class BlobWriteChannelImpl implements BlobWriteChannel {
3636

3737
private static final long serialVersionUID = 8675286882724938737L;
3838
private static final int MIN_CHUNK_SIZE = 256 * 1024;
@@ -50,12 +50,12 @@ class BlobWriterChannelImpl implements BlobWriteChannel {
5050
private transient StorageRpc storageRpc;
5151
private transient StorageObject storageObject;
5252

53-
BlobWriterChannelImpl(StorageOptions options, BlobInfo blobInfo,
53+
BlobWriteChannelImpl(StorageOptions options, BlobInfo blobInfo,
5454
Map<StorageRpc.Option, ?> optionsMap) {
5555
this.options = options;
5656
this.blobInfo = blobInfo;
5757
initTransients();
58-
uploadId = options.storageRpc().open(storageObject, optionsMap);
58+
uploadId = storageRpc.open(storageObject, optionsMap);
5959
}
6060

6161
private void writeObject(ObjectOutputStream out) throws IOException {

0 commit comments

Comments
 (0)