001    package org.cumulus4j.store.model;
002    
003    import java.util.Iterator;
004    import java.util.ServiceLoader;
005    
006    import javax.jdo.identity.LongIdentity;
007    
008    public class ObjectIdFactory {
009    
010            private static ObjectIdFactory instance;
011            static {
012                    ServiceLoader<ObjectIdFactory> serviceLoader = ServiceLoader.load(ObjectIdFactory.class);
013                    Iterator<ObjectIdFactory> it = serviceLoader.iterator();
014                    if (!it.hasNext())
015                            throw new IllegalStateException("No ObjectIdFactory service registered!");
016    
017                    ObjectIdFactory objectIdFactory = it.next();
018    
019                    if (it.hasNext())
020                            throw new IllegalStateException("Multiple ObjectIdFactory services registered!");
021    
022                    instance = objectIdFactory;
023            }
024    
025            public static ObjectIdFactory getInstance() {
026                    return instance;
027            }
028    
029            /**
030             * @deprecated Do not use this constructor directly! It exists only for the ServiceLoader!
031             * Use {@link #getInstance()} instead!
032             */
033            @Deprecated
034            public ObjectIdFactory() { }
035    
036            public Object createObjectId(Class<?> entityClass, long id) {
037                    LongIdentity identity = new LongIdentity(entityClass, id);
038                    return identity;
039            }
040    
041    }