Code example
This Java™ code example is of an OSGi service that makes a connection to an external SQL server web server, by way of the following Cloud Manager portForwards
rule of the enableEnvironmentAdvancedNetworkingConfiguration operation.
...
"portForwards": [{
"name": "mysql.example.com",
"portDest": 3306,
"portOrig": 30001
}]
...
core/src/com/adobe/aem/wknd/examples/connections/impl/MySqlExternalServiceImpl.java
package com.adobe.aem.wknd.examples.core.connections.impl;
import com.adobe.aem.wknd.examples.core.connections.ExternalService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
@Component
@Designate(ocd = MySqlExternalServiceImpl.Config.class)
public class MySqlExternalServiceImpl implements ExternalService {
private static final Logger log = LoggerFactory.getLogger(MySqlExternalServiceImpl.class);
// Get the proxy host using the AEM_PROXY_HOST Java environment variable provided by AEM as a Cloud Service
private static final String PROXY_HOST = System.getenv().getOrDefault("AEM_PROXY_HOST", "proxy.tunnel");
// Use the port mapped to the external MySQL service in the Cloud Manager API call
private static final int PORT_FORWARDS_PORT_ORIG = 30001;
private Config config;
@Override
public boolean isAccessible() {
log.debug("MySQL connection URL: [ {} ]", getConnectionUrl(config));
// Establish a connection with the external MySQL service
// This MySQL connection URL is created in getConnection(..) which will use the AEM_PROXY_HOST is it exists, and the proxied port.
try (Connection connection = DriverManager.getConnection(getConnectionUrl(config), config.username(), config.password())) {
// Validate the connection
connection.isValid(1000);
// Close the connection, since this is just a simple connectivity check
connection.close();
// Return true if AEM could reach the external SQL service
return true;
} catch (SQLException e) {
log.error("Unable to establish an connection with MySQL service using connection URL [ {} ]", getConnectionUrl(config), e);
}
return false;
}
/**
* Create a connection string to the MySQL using the AEM-provided AEM_PROXY_HOST and portForwards.portOrg port
* defined in the Cloud Manager API mapping.
*
* @param config OSGi configuration object
* @return the MySQL connection URI
*/
private String getConnectionUrl(Config config) {
return String.format("jdbc:mysql://%s:%d/wknd-examples", PROXY_HOST, PORT_FORWARDS_PORT_ORIG);
}
@Activate
protected void activate(Config config) throws ClassNotFoundException, SQLException {
this.config = config;
// Load the required MySQL Driver class required for Java to make the connection
// The OSGi bundle that contains this driver is deployed via the project's all project
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
}
@ObjectClassDefinition
@interface Config {
@AttributeDefinition(type = AttributeType.STRING)
String username();
@AttributeDefinition(type = AttributeType.STRING)
String password();
}
}