01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: // Copyright (C) 2003,2004 by Object Mentor, Inc. All rights reserved.
04: // Released under the terms of the GNU General Public License version 2 or
05: // later.
06: package fit;
07:
08: import java.util.*;
09:
10: import fit.exception.*;
11:
12: // REFACTOR The fixture path is really the only part of this
13: // class that needs to be globally accessible.
14: public class FixtureLoader {
15: private static FixtureLoader instance;
16:
17: public static FixtureLoader instance() {
18: if (instance == null) {
19: instance = new FixtureLoader();
20: }
21:
22: return instance;
23: }
24:
25: public Set fixturePathElements = new HashSet() {
26: private static final long serialVersionUID = 1L;
27:
28: {
29: add("fit");
30: }
31: };
32:
33: public Fixture disgraceThenLoad(String tableName) throws Throwable {
34: FixtureName fixtureName = new FixtureName(tableName);
35: Fixture fixture = instantiateFirstValidFixtureClass(fixtureName);
36: addPackageToFixturePath(fixture);
37: return fixture;
38: }
39:
40: private void addPackageToFixturePath(Fixture fixture) {
41: Package fixturePackage = fixture.getClass().getPackage();
42: if (fixturePackage != null)
43: addPackageToPath(fixturePackage.getName());
44: }
45:
46: public void addPackageToPath(String name) {
47: fixturePathElements.add(name);
48: }
49:
50: private Fixture instantiateFixture(String fixtureName)
51: throws Throwable {
52: Class classForFixture = loadFixtureClass(fixtureName);
53: FixtureClass fixtureClass = new FixtureClass(classForFixture);
54: return fixtureClass.newInstance();
55: }
56:
57: private Class loadFixtureClass(String fixtureName) {
58: try {
59: return Class.forName(fixtureName);
60: } catch (ClassNotFoundException deadEnd) {
61: if (deadEnd.getMessage().equals(fixtureName))
62: throw new NoSuchFixtureException(fixtureName);
63: else
64: throw new CouldNotLoadComponentFitFailureException(
65: deadEnd.getMessage(), fixtureName);
66: }
67: }
68:
69: private Fixture instantiateFirstValidFixtureClass(
70: FixtureName fixtureName) throws Throwable {
71: for (Iterator i = fixtureName.getPotentialFixtureClassNames(
72: fixturePathElements).iterator(); i.hasNext();) {
73: String each = (String) i.next();
74: try {
75: return instantiateFixture(each);
76: } catch (NoSuchFixtureException ignoreAndTryTheNextCandidate) {
77: }
78: }
79:
80: throw new NoSuchFixtureException(fixtureName.toString());
81: }
82: }
|