HibernateFederationDatabase.java from GridBlocks at Krugle
Show HibernateFederationDatabase.java syntax highlighted
/**
* Copyright (c) 2005
* Helsinki Institute of Physics
* see LICENSE file for details
*
* HibernateFederationDatabase.java
* Created on Nov 1, 2005
*/
package fi.hip.gb.gridlib.gridsp.userbase;
import fi.hip.gb.gridlib.idff12.database.spfedbase.FederationDatabase;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.io.*;
import java.util.List;
import java.util.Date;
import org.sourceid.idff12.adapter.FederationInfo;
/**
* This class includes the Hibernate implementation for the FederationDatabase
* interface.
* @author Henri Mikkonen
* @version $Id: $
*/
public class HibernateFederationDatabase implements FederationDatabase {
private static final SessionFactory factory;
private static final String hbrConfig =
"federationdatabase.hibernate.cfg.xml";
private static final String hbrMappings =
"HibernateFederationDatabase.hbm.xml";
private int id;
private String uid;
private FederationInfo fedInfo;
private String proxy;
static {
try {
InputStream ins =
Thread.currentThread().
getContextClassLoader().getResourceAsStream(hbrMappings);
Configuration cfg = new Configuration().configure(hbrConfig);
cfg.addInputStream(ins);
factory = cfg.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
private static final ThreadLocal session = new ThreadLocal();
/**
* Gets current Hibernate session.
* @return Hibernate session.
* @throws HibernateException If something goes wrong.
*/
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
s = factory.openSession();
session.set(s);
}
return s;
}
/**
* Closes the current Hibernate session.
* @throws HibernateException If something goes wrong.
*/
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
/**
* Constructor. No operation.
*/
public HibernateFederationDatabase() {
}
private void removeDatabaseElement(int idValue) {
Session session = currentSession();
Transaction tx = session.beginTransaction();
Query query =
session.createQuery("from fi.hip.gb.gridlib.gridsp.userbase.HibernateFederationDatabase as db where db.id = '" + idValue + "'");
session.delete(query.uniqueResult());
tx.commit();
closeSession();
}
private void updateDatabaseElement(int idValue, FederationInfo info) {
Session session = currentSession();
Transaction tx = session.beginTransaction();
Query query =
session.createQuery("from fi.hip.gb.gridlib.gridsp.userbase.HibernateFederationDatabase as db where db.id = '" + idValue + "'");
HibernateFederationDatabase entry =
(HibernateFederationDatabase) query.uniqueResult();
entry.setFedInfo(info);
session.update(entry);
tx.commit();
closeSession();
}
private List getDatabaseElements(String key, String value) {
Session session = currentSession();
Transaction tx = session.beginTransaction();
Query query =
session.createQuery("from fi.hip.gb.gridlib.gridsp.userbase.HibernateFederationDatabase as db where db." + key + " = '" + value + "'");
List result = query.list();
tx.commit();
closeSession();
return result;
}
private List getAllDatabaseElements() {
Session session = currentSession();
Transaction tx = session.beginTransaction();
Query query =
session.createQuery("from fi.hip.gb.gridlib.gridsp.userbase.HibernateFederationDatabase");
List result = query.list();
tx.commit();
closeSession();
return result;
}
/**
* Checks whether a user with given identifier is federated with a
* provider.
* @param id The user identifier as <code>String</code>.
* @param providerId The provider identifier as <code>String</code>.
* @return <code>true</code> if federated, <code>false</code> otherwise.
*/
public boolean isFederatedWith(String id, String providerId) {
List elements = this.getDatabaseElements("uid", id);
for (int i = 0; i < elements.size(); i++) {
HibernateFederationDatabase db =
(HibernateFederationDatabase)elements.get(i);
FederationInfo fedInfo = db.getFedInfo();
if (fedInfo.getProviderId().equals(providerId)) {
return true;
}
}
return false;
}
/**
* Gets all the user federations by the user identifier.
* @param identifier The user identifier as <code>String</code>.
* @return All the federations of the user.
*/
public FederationInfo[] getFederationInfosByUid(String identifier) {
List elements = this.getDatabaseElements("uid", identifier);
FederationInfo[] result = new FederationInfo[elements.size()];
for (int i = 0; i < result.length; i++) {
HibernateFederationDatabase db =
(HibernateFederationDatabase)elements.get(i);
result[i] = db.getFedInfo();
}
return result;
}
/**
* Gets the federation by user's pseudonym handle at the IDP.
* @param nameId The user pseudonym at the IDP as <code>String</code>.
* @param providerId The IDP identifier as <code>String</code>.
* @return The corresponding federation, <code>null</code> if it does
* not exist.
*/
public FederationInfo getFederationInfoByIdp(String nameId,
String providerId) {
List elements = this.getAllDatabaseElements();
for (int i = 0; i < elements.size(); i++) {
HibernateFederationDatabase db =
(HibernateFederationDatabase)elements.get(i);
FederationInfo fedInfo = db.getFedInfo();
if (fedInfo.getIdpNameId().equals(nameId) &&
fedInfo.getProviderId().equals(providerId)) {
return fedInfo;
}
}
return null;
}
/**
* Gets the federation by user's pseudonym handle at the SP.
* @param nameId The user pseudonym at the SP as <code>String</code>.
* @param providerId The SP identifier as <code>String</code>.
* @return The corresponding federation, <code>null</code> if it does
* not exist.
*/
public FederationInfo getFederationInfoBySp(String nameId,
String providerId) {
List elements = this.getAllDatabaseElements();
for (int i = 0; i < elements.size(); i++) {
HibernateFederationDatabase db =
(HibernateFederationDatabase)elements.get(i);
FederationInfo fedInfo = db.getFedInfo();
if (fedInfo.getSpNameId() != null) {
if (fedInfo.getSpNameId().equals(nameId) &&
fedInfo.getProviderId().equals(providerId)) {
return fedInfo;
}
}
}
return null;
}
/**
* Adds a federation.
* @param ide The user identifier in the database as <code>String</code>.
* @param principalId The user's local identifier as <code>String</code>.
* @param providerId The provider identifier as <code>String</code>.
* @param idpNameId The user's pseudonym at the IDP as <code>String</code>.
* @return The corresponding federation.
*/
public FederationInfo addFederation(String ide,
String principalId,
String providerId,
String idpNameId) {
Session session = currentSession();
Transaction tx = session.beginTransaction();
HibernateFederationDatabase newEntry =
new HibernateFederationDatabase();
FederationInfo info = new FederationInfo(principalId,
providerId,
idpNameId);
newEntry.setUid(ide);
newEntry.setFedInfo(info);
session.save(newEntry);
tx.commit();
closeSession();
return info;
}
/**
* Removes a federation.
* @param principalId The user's local identifier as <code>String</code>.
* @param providerId The provider identifier as <code>String</code>.
*/
public void removeFederation(String principalId, String providerId) {
List elements = this.getAllDatabaseElements();
for (int i = 0; i < elements.size(); i++) {
HibernateFederationDatabase element =
(HibernateFederationDatabase)elements.get(i);
FederationInfo fedInfo = element.getFedInfo();
if (fedInfo.getPrincipalId().equals(principalId) &&
fedInfo.getProviderId().equals(providerId)) {
this.removeDatabaseElement(element.getId());
}
}
}
/**
* Updates the user's pseudonym handle at the SP.
* @param providerId The SP identifier as <code>String</code>.
* @param idpNameId The user's pseudonym at the IDP as <code>String</code>.
* @param oldSpNameId The user's old (current) pseudonym at the SP as
* <code>String</code>.
* @param newSpNameId The user's new pseudonym at the SP as
* <code>String</code>.
*/
public void updateSpNameId(String providerId,
String idpNameId,
String oldSpNameId,
String newSpNameId) {
List elements = this.getAllDatabaseElements();
for (int i = 0; i < elements.size(); i++) {
HibernateFederationDatabase element =
(HibernateFederationDatabase)elements.get(i);
FederationInfo fedInfo = element.getFedInfo();
if (fedInfo.getProviderId().equals(providerId) &&
fedInfo.getIdpNameId().equals(idpNameId)) {
fedInfo.setSpNameId(newSpNameId);
this.updateDatabaseElement(element.getId(), fedInfo);
}
}
}
/**
* Updates the user's pseudonym handle at the IDP.
* @param providerId The IDP identifier as <code>String</code>.
* @param oldIdpNameId The user's old (current) pseudonym at the SP as
* <code>String</code>.
* @param newIdpNameId The user's new pseudonym at the IDP as
* <code>String</code>.
*/
public void updateIdpNameId(String providerId,
String oldIdpNameId,
String newIdpNameId) {
List elements = this.getAllDatabaseElements();
for (int i = 0; i < elements.size(); i++) {
HibernateFederationDatabase element =
(HibernateFederationDatabase)elements.get(i);
FederationInfo fedInfo = element.getFedInfo();
if (fedInfo.getProviderId().equals(providerId) &&
fedInfo.getIdpNameId().equals(oldIdpNameId)) {
fedInfo.setIdpNameId(newIdpNameId);
this.updateDatabaseElement(element.getId(), fedInfo);
}
}
}
/**
* Gets the authorized proxy certificate subject DN.
* @param principalId The user's local identifier as <code>String</code>.
* @param providerId The provider identifier as <code>String</code>.
* @return The proxy certificate subject DN as <code>String</code>.
*/
public String getProxyCertificateSubjectDN(String principalId,
String providerId) {
List elements = this.getAllDatabaseElements();
for (int i = 0; i < elements.size(); i++) {
HibernateFederationDatabase element =
(HibernateFederationDatabase)elements.get(i);
FederationInfo fedInfo = element.getFedInfo();
if (fedInfo.getPrincipalId().equals(principalId) &&
fedInfo.getProviderId().equals(providerId)) {
System.out.println(principalId + " db: " +
fedInfo.getPrincipalId());
String[] subjects = fedInfo.getCertSubjects();
if (subjects.length > 0) {
return subjects[0];
}
}
}
return null;
}
/**
* Sets the authorized proxy certificate subject DN.
* @param principalId The user's local identifier as <code>String</code>.
* @param providerId The provider identifier as <code>String</code>.
* @param subjectDN The proxy certificate subject DN as
* <code>String</code>.
*/
public void setProxyCertificateSubjectDN(String principalId,
String providerId,
String subjectDN) {
System.out.println("set proxy subject for " + principalId +
" " + providerId + " " + subjectDN);
String dn = this.getProxyCertificateSubjectDN(principalId, providerId);
List elements = this.getAllDatabaseElements();
for (int i = 0; i < elements.size(); i++) {
HibernateFederationDatabase element =
(HibernateFederationDatabase)elements.get(i);
FederationInfo fedInfo = element.getFedInfo();
if (fedInfo.getPrincipalId().equals(principalId) &&
fedInfo.getProviderId().equals(providerId)) {
if (dn != null) {
fedInfo.removeCertSubject(dn);
}
fedInfo.addCertSubject(subjectDN);
this.updateDatabaseElement(element.getId(), fedInfo);
}
}
}
/**
* Gets the user proxy.
* @param identifier The user's identifier at the database as
* <code>String</code>.
* @return The user proxy as Base64-encoded <code>String</code>,
* <code>null</code> if it does not exist.
*/
public String getUserProxy(String identifier) {
PortalUser user = new PortalUser();
user = user.getUserByName(identifier);
String uid = user.getUid().toString();
List list = this.getDatabaseElements("uid", uid);
for (int i = 0; i < list.size(); i++) {
HibernateFederationDatabase db =
(HibernateFederationDatabase)list.get(i);
String uProxy = db.getProxy();
if (uProxy != null) {
return uProxy;
}
}
return null;
}
/**
* Sets the user proxy for a federation.
* @param nameId The user's pseudonym at the IDP as <code>String</code>.
* @param providerId The IDP identifier as <code>String</code>.
* @param newProxy The new user proxy as Base64-encoded
* <code>String</code>.
*/
public void setUserProxy(String nameId,
String providerId,
String newProxy) {
List elements = this.getAllDatabaseElements();
HibernateFederationDatabase fedBase = null;
for (int i = 0; i < elements.size(); i++) {
HibernateFederationDatabase db =
(HibernateFederationDatabase)elements.get(i);
FederationInfo fedInfo = db.getFedInfo();
if (fedInfo.getIdpNameId().equals(nameId) &&
fedInfo.getProviderId().equals(providerId)) {
fedBase = db;
}
}
if (fedBase != null) {
Session session = currentSession();
Transaction tx = session.beginTransaction();
Query query =
session.createQuery("from fi.hip.gb.gridlib.gridsp.userbase.HibernateFederationDatabase as db where db.id = '" + fedBase.getId() + "'");
HibernateFederationDatabase entry =
(HibernateFederationDatabase)query.uniqueResult();
entry.setProxy(newProxy);
session.update(entry);
tx.commit();
closeSession();
}
}
// Methods required by Hibernate
/**
* Sets the user's database identifier. Required by Hibernate.
* @param id The database identifier as <code>int</code>.
*/
public void setId(int id) {
this.id = id;
}
/**
* Gets the user's database identifier. Required by Hibernate.
* @return The database identifier as <code>int</code>.
*/
public int getId() {
return this.id;
}
/**
* Sets the user identifier. Required by Hibernate.
* @param id The identifier as <code>String</code>.
*/
public void setUid(String id) {
this.uid = id;
}
/**
* Gets the user identifier. Required by Hibernate.
* @return The identifier as <code>String</code>.
*/
public String getUid() {
return this.uid;
}
/**
* Sets the Federation info. Required by Hibernate.
* @param info The federation info.
*/
public void setFedInfo(FederationInfo info) {
this.fedInfo = info;
}
/**
* Gets the Federation info. Required by Hibernate.
* @return The federation info.
*/
public FederationInfo getFedInfo() {
return this.fedInfo;
}
/**
* Gets the user proxy. Required by Hibernate.
* @return The user proxy as Base64-encoded <code>String</code>.
*/
public String getProxy() {
return this.proxy;
}
/**
* Sets the user proxy. Required by Hibernate.
* @param newProxy The user proxy as Base64-encoded <code>String</code>.
*/
public void setProxy(String newProxy) {
this.proxy = newProxy;
}
}
See more files for this project here