Get a reference to the singleton factory. @return BagFactory
()
| 50 | * @return BagFactory |
| 51 | */ |
| 52 | public static BagFactory getInstance() { |
| 53 | if (gSelf == null) { |
| 54 | String factoryName = |
| 55 | System.getProperty("pig.data.bag.factory.name"); |
| 56 | String factoryJar = |
| 57 | System.getProperty("pig.data.bag.factory.jar"); |
| 58 | if (factoryName != null && factoryJar != null) { |
| 59 | try { |
| 60 | URL[] urls = new URL[1]; |
| 61 | urls[0] = new URL(factoryJar); |
| 62 | ClassLoader loader = new URLClassLoader(urls, |
| 63 | BagFactory.class.getClassLoader()); |
| 64 | Class c = Class.forName(factoryName, true, loader); |
| 65 | Object o = c.newInstance(); |
| 66 | if (!(o instanceof BagFactory)) { |
| 67 | throw new RuntimeException("Provided factory " + |
| 68 | factoryName + " does not extend BagFactory!"); |
| 69 | } |
| 70 | gSelf = (BagFactory)o; |
| 71 | } catch (Exception e) { |
| 72 | if (e instanceof RuntimeException) { |
| 73 | // We just threw this |
| 74 | RuntimeException re = (RuntimeException)e; |
| 75 | throw re; |
| 76 | } |
| 77 | throw new RuntimeException("Unable to instantiate " |
| 78 | + "bag factory " + factoryName, e); |
| 79 | } |
| 80 | } else { |
| 81 | gSelf = new DefaultBagFactory(); |
| 82 | } |
| 83 | } |
| 84 | return gSelf; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get a default (unordered, not distinct) data bag. |