Skip to content

Commit f835079

Browse files
Mark PowersMark Powers
Mark Powers
authored and
Mark Powers
committed
Initial commit
0 parents  commit f835079

File tree

82 files changed

+2012
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2012
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

9781484251935.jpg

38.3 KB
Loading

Contributing.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Contributing to Apress Source Code
2+
3+
Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers.
4+
5+
## How to Contribute
6+
7+
1. Make sure you have a GitHub account.
8+
2. Fork the repository for the relevant book.
9+
3. Create a new branch on which to make your change, e.g.
10+
`git checkout -b my_code_contribution`
11+
4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted.
12+
5. Submit a pull request.
13+
14+
Thank you for your contribution!

LICENSE.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Freeware License, some rights reserved
2+
3+
Copyright (c) 2020 P.J. McNerney
4+
5+
Permission is hereby granted, free of charge, to anyone obtaining a copy
6+
of this software and associated documentation files (the "Software"),
7+
to work with the Software within the limits of freeware distribution and fair use.
8+
This includes the rights to use, copy, and modify the Software for personal use.
9+
Users are also allowed and encouraged to submit corrections and modifications
10+
to the Software for the benefit of other users.
11+
12+
It is not allowed to reuse, modify, or redistribute the Software for
13+
commercial use in any way, or for a user’s educational materials such as books
14+
or blog articles without prior permission from the copyright holder.
15+
16+
The above copyright notice and this permission notice need to be included
17+
in all copies or substantial portions of the software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
27+

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Apress Source Code
2+
3+
This repository accompanies [*Beginning Bazel*](https://www.apress.com/9781484251935) by P.J. McNerney (Apress, 2020).
4+
5+
[comment]: #cover
6+
![Cover image](9781484251935.jpg)
7+
8+
Download the files as a zip using the green button, or clone the repository to your machine using Git.
9+
10+
## Releases
11+
12+
Release v1.0 corresponds to the code in the published book, without corrections or updates.
13+
14+
## Contributions
15+
16+
See the file Contributing.md for more information on how you can contribute to this repository.

chapter_03/WORKSPACE

Whitespace-only changes.

chapter_03/src/BUILD

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
java_binary(
2+
name = "HelloWorld",
3+
srcs = [
4+
"HelloWorld.java",
5+
],
6+
deps = [
7+
":LibraryExample"
8+
]
9+
)
10+
11+
java_library(
12+
name = "LibraryExample",
13+
srcs = ["IntMultiplier.java"],
14+
)
15+
16+
java_test(
17+
name = "LibraryExampleTest",
18+
srcs = [
19+
"IntMultiplierTest.java",
20+
],
21+
deps = [
22+
":LibraryExample",
23+
"//third_party:junit4",
24+
],
25+
test_class = "IntMultiplierTest",
26+
)

chapter_03/src/HelloWorld.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
public class HelloWorld {
3+
public static void main(String[] args) {
4+
System.out.println("Hello, World!");
5+
IntMultiplier im = new IntMultiplier(3, 4);
6+
System.out.println(im.GetProduct());
7+
}
8+
}

chapter_03/src/IntMultiplier.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class IntMultiplier {
2+
private int a;
3+
private int b;
4+
5+
public IntMultiplier(int a, int b) {
6+
this.a = a;
7+
this.b = b;
8+
}
9+
10+
public int GetProduct() {
11+
return a * b;
12+
}
13+
}

chapter_03/src/IntMultiplierTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import static org.junit.Assert.assertEquals;
2+
3+
import org.junit.Test;
4+
5+
public class IntMultiplierTest {
6+
7+
@Test
8+
public void testIntMultiplier() throws Exception {
9+
IntMultiplier im = new IntMultiplier(3, 4);
10+
assertEquals(12, im.GetProduct());
11+
}
12+
13+
@Test
14+
public void testIntMultiplier2() throws Exception {
15+
IntMultiplier im = new IntMultiplier(4, 5);
16+
assertEquals(20, im.GetProduct());
17+
}
18+
19+
}

chapter_03/third_party/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
java_import(
4+
name = "junit4",
5+
jars = [
6+
"hamcrest/hamcrest-core-1.3.jar",
7+
"junit/junit-4.12.jar",
8+
]
9+
)
Binary file not shown.
308 KB
Binary file not shown.

chapter_04/WORKSPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
http_archive(
3+
name = "io_bazel_rules_go",
4+
urls = ["https://github.com/bazelbuild/rules_go/releases/download/v0.19.5/rules_go-v0.19.5.tar.gz"],
5+
)
6+
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
7+
go_rules_dependencies()
8+
go_register_toolchains()

chapter_04/src/BUILD

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
2+
3+
java_binary(
4+
name = "HelloWorld",
5+
srcs = [
6+
"HelloWorld.java",
7+
],
8+
deps = [
9+
":LibraryExample"
10+
]
11+
)
12+
13+
java_library(
14+
name = "LibraryExample",
15+
srcs = ["IntMultiplier.java"],
16+
)
17+
18+
java_test(
19+
name = "LibraryExampleTest",
20+
srcs = [
21+
"IntMultiplierTest.java",
22+
],
23+
deps = [
24+
":LibraryExample",
25+
"//third_party:junit4",
26+
],
27+
test_class = "IntMultiplierTest",
28+
)
29+
30+
go_binary(
31+
name = "hello_world_go",
32+
srcs = [
33+
"hello_world.go",
34+
],
35+
)

chapter_04/src/HelloWorld.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
public class HelloWorld {
3+
public static void main(String[] args) {
4+
System.out.println("Hello, World!");
5+
IntMultiplier im = new IntMultiplier(3, 4);
6+
System.out.println(im.GetProduct());
7+
}
8+
}

chapter_04/src/IntMultiplier.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class IntMultiplier {
2+
private int a;
3+
private int b;
4+
5+
public IntMultiplier(int a, int b) {
6+
this.a = a;
7+
this.b = b;
8+
}
9+
10+
public int GetProduct() {
11+
return a * b;
12+
}
13+
}

chapter_04/src/IntMultiplierTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import static org.junit.Assert.assertEquals;
2+
3+
import org.junit.Test;
4+
5+
public class IntMultiplierTest {
6+
7+
@Test
8+
public void testIntMultiplier() throws Exception {
9+
IntMultiplier im = new IntMultiplier(3, 4);
10+
assertEquals(12, im.GetProduct());
11+
}
12+
13+
@Test
14+
public void testIntMultiplier2() throws Exception {
15+
IntMultiplier im = new IntMultiplier(4, 5);
16+
assertEquals(20, im.GetProduct());
17+
}
18+
19+
}

chapter_04/src/hello_world.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("Hello, World!")
7+
}

chapter_04/third_party/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
java_import(
4+
name = "junit4",
5+
jars = [
6+
"hamcrest/hamcrest-core-1.3.jar",
7+
"junit/junit-4.12.jar",
8+
]
9+
)
Binary file not shown.
308 KB
Binary file not shown.

chapter_05/WORKSPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
http_archive(
3+
name = "io_bazel_rules_go",
4+
urls = ["https://github.com/bazelbuild/rules_go/releases/download/v0.19.5/rules_go-v0.19.5.tar.gz"],
5+
)
6+
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
7+
go_rules_dependencies()
8+
go_register_toolchains()

chapter_05/src/BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
2+
3+
go_binary(
4+
name = "echo_server",
5+
srcs = ["echo_server.go"],
6+
deps = [":transmission_object_go"],
7+
)
8+
9+
go_library(
10+
name = "transmission_object_go",
11+
srcs = ["transmission_object.go"],
12+
importpath = "transmission_object",
13+
)
14+
15+
java_library(
16+
name = "transmission_object_java",
17+
srcs = ["TransmissionObject.java"],
18+
)
19+
20+
java_binary(
21+
name = "echo_client",
22+
srcs = ["EchoClient.java"],
23+
main_class = "EchoClient",
24+
deps = [
25+
":transmission_object_java",
26+
"//third_party:gson",
27+
]
28+
)

chapter_05/src/EchoClient.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.io.PrintWriter;
4+
import java.net.Socket;
5+
6+
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
8+
9+
public class EchoClient {
10+
private static final String kHostName = "localhost";
11+
private static final int kPortNumber = 1234;
12+
13+
public static void main (String args[]) {
14+
System.out.println("Spinning up the Echo Client in Java...");
15+
try {
16+
final Socket socketToServer = new Socket(kHostName, kPortNumber);
17+
final BufferedReader inputFromServer = new BufferedReader(new InputStreamReader(socketToServer.getInputStream()));
18+
final BufferedReader commandLineInput = new BufferedReader(new InputStreamReader(System.in));
19+
20+
System.out.println("Waiting on input from the user...");
21+
final String inputFromUser = commandLineInput.readLine();
22+
if (inputFromUser != null) {
23+
System.out.println("Received by Java: " + inputFromUser);
24+
25+
TransmissionObject transmissionObject = new TransmissionObject();
26+
transmissionObject.message = inputFromUser;
27+
transmissionObject.value = 3.145f;
28+
GsonBuilder builder = new GsonBuilder();
29+
Gson gson = builder.create();
30+
final PrintWriter outputToServer = new PrintWriter(socketToServer.getOutputStream(), true);
31+
outputToServer.println(gson.toJson(transmissionObject));
32+
System.out.println(inputFromServer.readLine());
33+
}
34+
socketToServer.close();
35+
} catch (Exception e) {
36+
System.err.println("Error: " + e);
37+
}
38+
}
39+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class TransmissionObject {
2+
public String message;
3+
public float value;
4+
}

chapter_05/src/echo_server.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"net"
8+
"transmission_object"
9+
)
10+
11+
func main() {
12+
log.Println("Spinning up the Echo Server in Go...")
13+
listen, error := net.Listen("tcp", ":1234")
14+
if error != nil {
15+
log.Panicln("Unable to listen: " + error.Error())
16+
}
17+
defer listen.Close()
18+
19+
connection, error := listen.Accept()
20+
if error != nil {
21+
log.Panicln("Cannot accept a connection! Error: " + error.Error())
22+
}
23+
24+
log.Println("Receiving on a new connection")
25+
defer connection.Close()
26+
defer log.Println("Connection now closed.")
27+
28+
buffer := make([]byte, 2048)
29+
size, error := connection.Read(buffer)
30+
if error != nil {
31+
log.Panicln("Unable to read from the buffer! Error: " + error.Error())
32+
}
33+
data := buffer[:size]
34+
var transmissionObject transmission_object.TransmissionObject
35+
error = json.Unmarshal(data, &transmissionObject)
36+
if error != nil {
37+
log.Panicln("Unable to unmarshal the buffer! Error: " + error.Error())
38+
}
39+
40+
log.Println("Message = " + transmissionObject.Message)
41+
log.Println("Value = " + fmt.Sprintf("%f", transmissionObject.Value))
42+
43+
transmissionObject.Message = "Echoed from Go: " + transmissionObject.Message
44+
transmissionObject.Value = 2 * transmissionObject.Value
45+
46+
message, error := json.Marshal(transmissionObject)
47+
if error != nil {
48+
log.Panicln("Unable to marshall the object! Error: " + error.Error())
49+
}
50+
connection.Write(message)
51+
}

0 commit comments

Comments
 (0)