-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathStudentServiceClient.java
74 lines (65 loc) · 2.64 KB
/
StudentServiceClient.java
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
/*******************************************************************************
* Copyright (c) 2015 Composent, Inc. and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Composent, Inc. - initial API and implementation
******************************************************************************/
package com.mycorp.examples.student.client;
import java.util.List;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferencePolicy;
import com.mycorp.examples.student.Address;
import com.mycorp.examples.student.Student;
import com.mycorp.examples.student.StudentService;
@Component(immediate = true)
public class StudentServiceClient {
private StudentService studentService;
@Reference(policy=ReferencePolicy.DYNAMIC)
void bindStudentService(StudentService service) throws Exception {
this.studentService = service;
System.out.println("Discovered student service=" + this.studentService);
// Get all students
List<Student> students = studentService.getStudents();
// Get first student from list
Student s0 = students.get(0);
// Print out first student
System.out.println("Student0=" + s0);
// If there is anyone there, then update
if (s0 != null) {
s0.setGrade("Eighth");
// And update
System.out.println("Updated Student0=" + studentService.updateStudent(s0));
}
// Get student with completablefuture
studentService.getStudentsCF().whenComplete((s, except) -> {
if (except != null)
except.printStackTrace();
else
System.out.println("getStudentCF().Student0=" + s.get(0));
});
// Create a new student
Student newstudent = studentService.createStudent("April Snow");
System.out.println("Created student=" + newstudent);
// when done change the grade to first
newstudent.setGrade("First");
Address addr = new Address();
addr.setStreet("111 NE 1st");
addr.setCity("Austin");
addr.setState("Oregon");
addr.setPostalCode("97200");
newstudent.setAddress(addr);
// update
Student updatednewstudent = studentService.updateStudent(newstudent);
System.out.println("Updated student=" + updatednewstudent);
// Then delete new student
Student deletedstudent = studentService.deleteStudent(updatednewstudent.getId());
System.out.println("Deleted student=" + deletedstudent);
}
void unbindStudentService(StudentService service) throws Exception {
this.studentService = null;
}
}