package demo; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.util.Hashtable; import java.util.Properties; 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 oracle.ucp.jdbc.ConnectionLabelingCallback; import oracle.ucp.jdbc.LabelableConnection; import weblogic.jdbc.extensions.WLDataSource; public class Pdb extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { public static String tenent = "TENANT"; 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 { 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"); MyConnectionLabelingCallback callback = new MyConnectionLabelingCallback(); ((WLDataSource)ds).registerConnectionLabelingCallback(callback); } 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("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); Connection conn = ((WLDataSource)ds).getConnection(props); conn.close(); } } class MyConnectionLabelingCallback implements ConnectionLabelingCallback { public MyConnectionLabelingCallback() { } public int cost(Properties reqLabels, Properties currentLabels) { // Case 1: exact match if (reqLabels.equals(currentLabels)) { System.out.println("## Exact match found = "+ (String) reqLabels.get(demo.Pdb.tenent)); return 0; } // Case 2: some labels match with no unmatched labels String val1 = (String) reqLabels.get(demo.Pdb.tenent); String val2 = (String) currentLabels.get(demo.Pdb.tenent); // No label matches to application's preference. // Do not choose this connection. System.out.println("## No match '"+val1+"' != '"+val2+"'##"); return Integer.MAX_VALUE; } public boolean configure(Properties reqLabels, Object conn) { // Only called if not a match try { String valStr = (String) reqLabels.get(demo.Pdb.tenent); Statement s = ((Connection)conn).createStatement(); s.executeUpdate("ALTER SESSION SET CONTAINER = "+valStr); LabelableConnection lconn = (LabelableConnection) conn; System.out.println("## In configure for value "+valStr); lconn.applyConnectionLabel(demo.Pdb.tenent, valStr); } catch (Exception exc) { exc.printStackTrace(); return false; } return true; } public java.util.Properties getRequestedLabels() { Properties props = new Properties(); props.put(demo.Pdb.tenent, "value"); return props; } }