Skip to content

Commit d353b4f

Browse files
author
bengtson.lennart
committed
Fixed issue with initialization of the service
1 parent 6ac8bd6 commit d353b4f

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lrsDomain =
2+
lrsAuth =

src/main/i5/las2peer/services/privacyControl/PrivacyControlService.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@
2828
import javax.ws.rs.Produces;
2929
import javax.ws.rs.core.MediaType;
3030
import javax.ws.rs.core.Response;
31+
import javax.ws.rs.core.Response.Status;
3132

3233
import org.w3c.dom.Element;
3334
import org.web3j.tuples.generated.Tuple4;
3435

36+
import i5.las2peer.api.Context;
37+
import i5.las2peer.api.ManualDeployment;
3538
import i5.las2peer.api.security.AgentException;
3639
import i5.las2peer.api.security.AgentNotFoundException;
3740
import i5.las2peer.api.security.AgentOperationFailedException;
@@ -81,14 +84,15 @@
8184
url = "rwth-aachen.de",
8285
email = "lennart.bengtson@rwth-aachen.de")))
8386
@ServicePath("/privacy")
87+
@ManualDeployment
8488
public class PrivacyControlService extends RESTService {
8589

8690
private final static L2pLogger logger = L2pLogger.getInstance(PrivacyControlService.class.getName());
8791

8892
private final static String DEFAULT_CONFIG_FILE = "etc/consentConfiguration.xml";
8993

90-
private static String lrsDomain;
91-
private static String lrsAuth;
94+
private String lrsDomain;
95+
private String lrsAuth;
9296
private static ConcurrentMap<String, String> userToMoodleToken = new ConcurrentHashMap<>();
9397

9498
private static EthereumNode node;
@@ -111,30 +115,20 @@ public class PrivacyControlService extends RESTService {
111115
// ------------------------------ Initialization -----------------------------
112116

113117
public PrivacyControlService() {
114-
super();
115-
116-
// Workaround, to avoid reloading the contracts.
117-
if (!initialized) {
118-
// Wait for service to be started before executing the initialization
119-
// Seems to be necessary because node (the service is running at) would not be known otherwise
120-
new java.util.Timer().schedule(
121-
new java.util.TimerTask() {
122-
@Override
123-
public void run() {
124-
init();
125-
}
126-
},
127-
10000
128-
);
129-
}
118+
setFieldValues();
130119
}
131120

132-
/**
133-
* Initializes the privacy control service instance.
134-
* Reads information about available consent levels from XML configuration file.
135-
* Loads/Deploys necessary smart contracts.
136-
*/
137-
public void init() {
121+
@POST
122+
@Path("/init")
123+
@Produces(MediaType.TEXT_PLAIN)
124+
@ApiResponses(
125+
value = { @ApiResponse(
126+
code = HttpURLConnection.HTTP_OK,
127+
message = "Privacy control service initialized") })
128+
public Response init() {
129+
if (initialized) {
130+
return Response.status(Status.BAD_REQUEST).entity("Already initialized").build();
131+
}
138132
logger.info("Initializing privacy control service...");
139133
// Read consent levels from configuration file.
140134
try {
@@ -148,6 +142,7 @@ public void init() {
148142
} catch (Exception e) {
149143
logger.warning("Unable to read from XML. Please make sure file exists and is correctly formatted.");
150144
e.printStackTrace();
145+
return Response.status(Status.BAD_REQUEST).entity("Unable to read consent configuration").build();
151146
}
152147
logger.info("Successfully read from XML consent configuration file.");
153148

@@ -162,7 +157,7 @@ public void init() {
162157

163158
// Deploy smart contracts from wrapper classes
164159
try {
165-
ServiceAgentImpl agent = (ServiceAgentImpl) this.getAgent();
160+
ServiceAgentImpl agent = (ServiceAgentImpl) Context.getCurrent().getServiceAgent();
166161
node = (EthereumNode) agent.getRunningAtNode();
167162
registryClient = node.getRegistryClient();
168163

@@ -172,9 +167,9 @@ public void init() {
172167
} catch (Exception e) {
173168
logger.warning("Initilization of smart contracts failed!");
174169
e.printStackTrace();
170+
return Response.status(Status.BAD_REQUEST).entity("Initilization of smart contracts failed!").build();
175171
}
176-
177-
logger.info("Done. Proceeding");
172+
return Response.status(Status.OK).entity("Initialization successful.").build();
178173
}
179174

180175
// ------------------------------ Bot communication ----------------------------

start.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ export SERVICE=${SERVICE_NAME}.${SERVICE_CLASS}@${SERVICE_VERSION}
114114

115115
[[ -z "${SERVICE_PASSPHRASE}" ]] && export SERVICE_PASSPHRASE='newPass'
116116

117+
# check mandatory variables
118+
[[ -z "${LRS_DOMAIN}" ]] && \
119+
echo "Mandatory variable LRS_DOMAIN is not set. Add -e LRS_DOMAIN=lrsDomain to your arguments." && exit 1
120+
[[ -z "${LRS_AUTH_ADMIN}" ]] && \
121+
echo "Mandatory variable LRS_AUTH is not set. Add -e LRS_AUTH_ADMIN=lrsAuthAdmin to your arguments." && exit 1
122+
123+
# configure service properties
124+
function set_in_service_config {
125+
sed -i "s?${1}[[:blank:]]*=.*?${1}=${2}?g" ${SERVICE_PROPERTY_FILE}
126+
}
127+
set_in_service_config lrsDomain ${LRS_DOMAIN}
128+
set_in_service_config lrsAuth "${LRS_AUTH_ADMIN}"
129+
117130
echo Starting las2peer node ...
118131
if [ -n "$LAS2PEER_ETH_HOST" ]; then
119132
echo ... using ethereum boot procedure:

0 commit comments

Comments
 (0)