-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathjava-client.html.md.erb
162 lines (131 loc) · 5.37 KB
/
java-client.html.md.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
title: Cloud Foundry Java Client Library
owner: Java
---
You can use the Cloud Foundry Java Client Library to manage an account on a Cloud Foundry instance.
Cloud Foundry Java Client Library v1.1.x works with apps using Spring v4.x, and
Cloud Foundry Java Client Library v1.0.x work with apps using Spring v3.x. Both versions are available in the [Cloud Foundry Java Client Library](https://github.com/cloudfoundry/cf-java-client) repository on GitHub.
## <a id='adding-the-library'></a> Adding the Java Client Library
To obtain the correct components, see the [Cloud Foundry Java Client Library](https://github.com/cloudfoundry/cf-java-client) repository on GitHub.
Most projects need two dependencies: the Operations API and an implementation of the Client API.
For more information about how to add the Cloud Foundry Java Client Library as dependencies to a Maven or Gradle project, see the sections below.
### <a id='maven'></a> Maven
Add the `cloudfoundry-client-reactor` dependency (formerly known as `cloudfoundry-client-spring`) to your `pom.xml` as follows:
```
<dependencies>
<dependency>
<groupId>org.cloudfoundry</groupId>
<artifactId>cloudfoundry-client-reactor</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.cloudfoundry</groupId>
<artifactId>cloudfoundry-operations</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>2.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-netty</artifactId>
<version>2.5.0.BUILD-SNAPSHOT</version>
</dependency>
...
</dependencies>
```
The artifacts can be found in the Spring release and snapshot repositories:
```
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/release</url>
</repository>
...
</repositories>
```
```
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
...
</repositories>
```
### <a id='gradle'></a> Gradle
Add the `cloudfoundry-client-reactor` dependency to your `build.gradle` file as follows:
```
dependencies {
compile 'org.cloudfoundry:cloudfoundry-client-reactor:2.0.0.BUILD-SNAPSHOT'
compile 'org.cloudfoundry:cloudfoundry-operations:2.0.0.BUILD-SNAPSHOT'
compile 'io.projectreactor:reactor-core:2.5.0.BUILD-SNAPSHOT'
compile 'io.projectreactor:reactor-netty:2.5.0.BUILD-SNAPSHOT'
...
}
```
The artifacts can be found in the Spring release and snapshot repositories:
```
repositories {
maven { url 'http://repo.spring.io/release' }
...
}
```
```
repositories {
maven { url 'http://repo.spring.io/snapshot' }
...
}
```
## <a id='sample-code'></a> Sample code
The following is a very simple sample app that connects to a Cloud Foundry instance, logs in, and displays some information about the Cloud Foundry account. When running the program, provide the Cloud Foundry target API endpoint, along with a valid user name and password as command-line parameters.
```
import org.cloudfoundry.client.lib.CloudCredentials;
import org.cloudfoundry.client.lib.CloudFoundryClient;
import org.cloudfoundry.client.lib.domain.CloudApplication;
import org.cloudfoundry.client.lib.domain.CloudService;
import org.cloudfoundry.client.lib.domain.CloudSpace;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public final class JavaSample {
public static void main(String[] args) {
String target = args[0];
String user = args[1];
String password = args[2];
CloudCredentials credentials = new CloudCredentials(user, password);
CloudFoundryClient client = new CloudFoundryClient(credentials, getTargetURL(target));
client.login();
System.out.printf("%nSpaces:%n");
for (CloudSpace space : client.getSpaces()) {
System.out.printf(" %s\t(%s)%n", space.getName(), space.getOrganization().getName());
}
System.out.printf("%nApplications:%n");
for (CloudApplication application : client.getApplications()) {
System.out.printf(" %s%n", application.getName());
}
System.out.printf("%nServices%n");
for (CloudService service : client.getServices()) {
System.out.printf(" %s\t(%s)%n", service.getName(), service.getLabel());
}
}
private static URL getTargetURL(String target) {
try {
return URI.create(target).toURL();
} catch (MalformedURLException e) {
throw new RuntimeException("The target URL is not valid: " + e.getMessage());
}
}
}
```
For more details about the Cloud Foundry Java Client Library, see the
[Cloud Foundry Java Client Library](https://github.com/cloudfoundry/cf-java-client) repository on GitHub.
To view the
objects that you can query and inspect, see [domain package](https://github.com/cloudfoundry/cf-java-client/tree/main/cloudfoundry-client/src/main/java/org/cloudfoundry/client/v2/domains) in the cloudfoundry/cf-java-client repository on GitHub.