package geniusweb.partiesserver.repository; import geniusweb.actions.PartyId; import geniusweb.party.Capabilities; import geniusweb.party.Party; /** * An approved and ready to run party. */ public class AvailableJavaParty implements AvailableParty { private final Class partyclass; private final String name; private final transient Capabilities capabilities; private final transient String description; /** * * @param name the filename. This is not a {@link PartyId}. * @param partyclass the class of the party. * @throws ClassNotFoundException if unknown partyclass */ public AvailableJavaParty(String name, Class partyclass) throws ClassNotFoundException { this.name = name; this.partyclass = partyclass; Party party; try { party = partyclass.newInstance(); } catch (Throwable e) { throw new ClassNotFoundException( "Could not instantiate " + partyclass, e); } capabilities = party.getCapabilities(); description = party.getDescription(); } @Override public String getID() { return name; } @Override public String getName() { return name; } /** * * @return cached copy of what was received from a call to * {@link Party#getDescription()}. */ @Override public String getDescription() { return description; } /** * * @return cached copy of what was received from a call to * {@link AvailableParty#getCapabilities()} */ @Override public Capabilities getCapabilities() { return capabilities; } @Override public Party getInstance() throws InstantiationFailedException { try { return partyclass.newInstance(); } catch (Exception e) { throw new InstantiationFailedException( "Failed to create instance of party " + this, e); } } @Override public void free() { // FAIK we can not unload the class or free resources. // it would be done automatically if our classloader // is garbage collected. } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((partyclass == null) ? 0 : partyclass.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; AvailableJavaParty other = (AvailableJavaParty) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (partyclass == null) { if (other.partyclass != null) return false; } else if (!partyclass.equals(other.partyclass)) return false; return true; } @Override public String toString() { return name + " (" + partyclass + ")"; } }