package demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.concurrent.Executor;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.CallableStatement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import javax.sql.rowset.JdbcRowSet;
import weblogic.jdbc.rowset.*;
import weblogic.jndi.Environment;
import oracle.ucp.jdbc.ConnectionLabelingCallback;
import oracle.ucp.jdbc.LabelableConnection;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleClob;
import java.util.Properties;
import java.util.Map;
import java.util.Hashtable;
import java.util.Set;
import weblogic.jdbc.extensions.WLDataSource;
import weblogic.transaction.TransactionManager;
import weblogic.transaction.TransactionHelper;
public class Pdb extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
public static String tenent = "TENANT";
public static String user = "USER";
static boolean didRegister = false;
TransactionManager tm = null;
DataSource ds = null;
public Pdb() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("");
writer.println("
Pdb");
writer.println(""+doit()+"");
writer.println("");
writer.close();
}
private void getDS() throws SQLException {
try {
//InitialContext ctx = new InitialContext();
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
ht.put(Context.SECURITY_PRINCIPAL, "weblogic");
ht.put(Context.SECURITY_CREDENTIALS, "welcome1");
InitialContext ctx = new InitialContext(ht);
ds = (DataSource)ctx.lookup("ds0");
tm = (TransactionManager)ctx.lookup("weblogic.transaction.TransactionManager");
if (!didRegister) {
MyConnectionLabelingCallback callback = new MyConnectionLabelingCallback();
((WLDataSource)ds).registerConnectionLabelingCallback(callback);
didRegister = true;
}
} catch(Throwable t) {
t.printStackTrace();
throw new SQLException("Can't get WLS DataSource");
}
}
private String doit() {
try {
getDS();
} catch (Exception ignore) {
ignore.printStackTrace();
return "Not run";
}
try {
doit2("PDB2");
doit2("PDB1");
doit2("PDB2");
doit2("PDB1");
doit2("PDB2");
doit2("PDB2");
} catch(Exception e) {
System.out.println("Failed to run");
e.printStackTrace();
return "Not run";
}
return ("Done");
}
public void doit2(String pdb) throws Exception {
Properties props = new Properties();
props.put(demo.Pdb.tenent, pdb);
props.put(demo.Pdb.user, pdb+"USER");
Connection conn = ((WLDataSource)ds).getConnection(props);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select user from dual");
if (rs.next()) {
System.out.println("User ="+rs.getString(1));
}
statement.close();
conn.close();
}
}
class MyConnectionLabelingCallback implements ConnectionLabelingCallback {
public MyConnectionLabelingCallback() {
}
public int cost(Properties reqLabels, Properties currentLabels) {
String tenant1 = (String) currentLabels.get(demo.Pdb.tenent);
String tenant2 = (String) reqLabels.get(demo.Pdb.tenent);
if (tenant1 != null && tenant2 != null && tenant1.equals(tenant2)) {
String user1 = (String) currentLabels.get(demo.Pdb.user);
String user2 = (String) reqLabels.get(demo.Pdb.user);
//if (user1 != null && user2 != null && user1.equals(user2)) {
//System.out.println("## Exact match found = "+ tenant1);
//return 0;
//}
return 1; // tenant matches but user doesn't
}
// No label matches to application's preference.
// Do not choose this connection.
System.out.println("## No match '"+tenant1+"' != '"+tenant2+"'##");
return Integer.MAX_VALUE;
}
public boolean configure(Properties reqLabels, Object conn) {
boolean tenantMatch = false;
LabelableConnection lconn = (LabelableConnection)conn;
OracleConnection oconn = (OracleConnection)conn;
Properties current = null;
try {
current = lconn.getConnectionLabels();
} catch (Exception ignore) {}
String user1 = null;
if (current != null)
user1 = (String) current.getProperty(demo.Pdb.user);
String user2 = (String) reqLabels.get(demo.Pdb.user);
String tenant1 = null;
if (current != null)
tenant1 = (String) current.getProperty(demo.Pdb.tenent);
String tenant2 = (String) reqLabels.get(demo.Pdb.tenent);
if (tenant1 != null && tenant2 != null && tenant1.equals(tenant2))
tenantMatch = true;
if (user1 != null) {
try {
lconn.removeConnectionLabel(demo.Pdb.user);
oconn.close(OracleConnection.PROXY_SESSION);
} catch (Exception exc) {
exc.printStackTrace();
}
}
if (!tenantMatch) {
try {
Statement s = ((Connection)conn).createStatement();
s.executeUpdate("ALTER SESSION SET CONTAINER = "+tenant2);
lconn.applyConnectionLabel(demo.Pdb.tenent, tenant2);
System.out.println("## In configure for value "+tenant2);
} catch (Exception exc) {
exc.printStackTrace();
return false;
}
}
if (user2 != null) {
try {
java.util.Properties prop = new java.util.Properties();
prop.put(OracleConnection.PROXY_USER_NAME, user2);
oconn.openProxySession(OracleConnection.PROXYTYPE_USER_NAME, prop);
lconn.applyConnectionLabel(demo.Pdb.user, user2);
} catch (Exception exc) {
System.out.println("Failed to set proxy to "+user2);
exc.printStackTrace();
return false;
}
}
return true;
}
public java.util.Properties getRequestedLabels() {
Properties props = new Properties();
props.put(demo.Pdb.tenent, "value");
return props;
}
}