Changeset 1341
- Timestamp:
- 05/19/11 08:40:46 (13 years ago)
- Location:
- ComponentRegistry/trunk/ComponentRegistry/src
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/CMDComponentSpecExpanderDbImpl.java
r1335 r1341 10 10 public class CMDComponentSpecExpanderDbImpl extends CMDComponentSpecExpander { 11 11 12 private ComponentRegistryDbImpl dbImplRegistry; 13 12 14 public CMDComponentSpecExpanderDbImpl(ComponentRegistryDbImpl registry) { 13 15 super(registry); 16 dbImplRegistry = registry; 14 17 } 15 18 … … 26 29 @Override 27 30 protected CMDComponentSpec getUncachedComponent(String componentId) { 28 return registry.getMDComponent(componentId);31 return dbImplRegistry.getUncachedMDComponent(componentId); 29 32 } 30 33 31 34 @Override 32 35 protected CMDComponentSpec getUncachedProfile(String profileId) { 33 return registry.getMDProfile(profileId);36 return dbImplRegistry.getUncachedMDProfile(profileId); 34 37 } 35 38 } -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java
r1340 r1341 26 26 import org.slf4j.LoggerFactory; 27 27 import org.springframework.beans.factory.annotation.Autowired; 28 import org.springframework.beans.factory.annotation.Qualifier; 28 29 import org.springframework.dao.DataAccessException; 29 30 … … 33 34 * @author Twan Goosen <twan.goosen@mpi.nl> 34 35 */ 35 public class ComponentRegistryDbImpl extends ComponentRegistryImplBase implements ComponentRegistry 36 public class ComponentRegistryDbImpl extends ComponentRegistryImplBase implements ComponentRegistry { 36 37 37 38 private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryDbImpl.class); … … 43 44 @Autowired 44 45 Configuration configuration; 46 @Autowired 47 @Qualifier("componentsCache") 48 CMDComponentSpecCache componentsCache; 49 @Autowired 50 @Qualifier("profilesCache") 51 CMDComponentSpecCache profilesCache; 45 52 46 53 /** … … 110 117 @Override 111 118 public CMDComponentSpec getMDProfile(String id) { 112 try { 113 return getMDComponent(id, profileDescriptionDao); 119 CMDComponentSpec result = profilesCache.get(id); 120 if (result == null && !profilesCache.containsKey(id)) { 121 result = getUncachedMDProfile(id); 122 profilesCache.put(id, result); 123 } 124 return result; 125 } 126 127 public CMDComponentSpec getUncachedMDProfile(String id) { 128 try { 129 return getUncachedMDComponent(id, profileDescriptionDao); 114 130 } catch (DataAccessException ex) { 115 131 LOG.error("Database access error while trying to get profile", ex); … … 120 136 @Override 121 137 public CMDComponentSpec getMDComponent(String id) { 122 try { 123 return getMDComponent(id, componentDescriptionDao); 138 CMDComponentSpec result = componentsCache.get(id); 139 if (result == null && !componentsCache.containsKey(id)) { 140 result = getUncachedMDComponent(id); 141 componentsCache.put(id, result); 142 } 143 return result; 144 } 145 146 public CMDComponentSpec getUncachedMDComponent(String id) { 147 try { 148 return getUncachedMDComponent(id, componentDescriptionDao); 124 149 } catch (DataAccessException ex) { 125 150 LOG.error("Database access error while trying to get component", ex); … … 138 163 componentDescriptionDao.insertDescription(description, xml, isPublic(), userId); 139 164 } 165 invalidateCache(description); 140 166 return 0; 141 167 } catch (JAXBException ex) { … … 154 180 AbstractDescriptionDao<?> dao = getDaoForDescription(description); 155 181 dao.updateDescription(getIdForDescription(description), description, componentSpecToString(spec)); 182 invalidateCache(description); 156 183 return 0; 157 184 } catch (DataAccessException ex) { … … 225 252 checkAge(desc, principal); 226 253 profileDescriptionDao.setDeleted(getIdForDescription(desc)); 254 invalidateCache(desc); 227 255 } 228 256 } catch (DataAccessException ex) { … … 246 274 // } 247 275 componentDescriptionDao.setDeleted(getIdForDescription(desc)); 276 invalidateCache(desc); 248 277 } 249 278 } catch (DataAccessException ex) { … … 275 304 public void setUserId(Number user) { 276 305 this.userId = user; 306 } 307 308 private void invalidateCache(AbstractDescription description){ 309 if(description.isProfile()){ 310 profilesCache.remove(description.getId()); 311 } else{ 312 componentsCache.remove(description.getId()); 313 } 277 314 } 278 315 … … 311 348 } 312 349 313 private CMDComponentSpec get MDComponent(String id, AbstractDescriptionDao dao) {350 private CMDComponentSpec getUncachedMDComponent(String id, AbstractDescriptionDao dao) { 314 351 String xml = dao.getContent(id); 315 352 if (xml != null) { … … 345 382 if (isPublic() && !configuration.isAdminUser(principal)) { 346 383 try { 347 Date regDate = AbstractDescription.getDate(desc.getRegistrationDate()); 384 Date regDate = AbstractDescription.getDate(desc. 385 getRegistrationDate()); 348 386 Calendar calendar = Calendar.getInstance(); 349 387 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); -
ComponentRegistry/trunk/ComponentRegistry/src/main/webresources/WEB-INF/spring-config/database-impl.xml
r1293 r1341 7 7 <import resource="persistence.xml"/> 8 8 9 <bean id="componentsCache" class="clarin.cmdi.componentregistry.impl.database.CMDComponentSpecCache" /> 10 <bean id="profilesCache" class="clarin.cmdi.componentregistry.impl.database.CMDComponentSpecCache" /> 11 9 12 <!-- Prototype bean for component registry (service) --> 10 <bean id="componentRegistry" class="clarin.cmdi.componentregistry.impl.database.ComponentRegistryDbImpl" 13 <bean id="componentRegistry" class="clarin.cmdi.componentregistry.impl.database.ComponentRegistryDbImpl" scope="prototype"></bean> 11 14 12 15 <!-- Factory that will create component registries for us (and autowire them), to be used by actual ComponentRegistryFactory --> 13 16 <bean id="componentRegistryBeanFactory" 14 17 class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"> 15 18 <property name="serviceLocatorInterface" value="clarin.cmdi.componentregistry.impl.database.ComponentRegistryBeanFactory"/> 16 19 </bean> 17 20 -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImplTest.java
r1337 r1341 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 4 import clarin.cmdi.componentregistry.DeleteFailedException; 5 import clarin.cmdi.componentregistry.MDMarshaller; 5 6 import clarin.cmdi.componentregistry.UserCredentials; 6 7 import clarin.cmdi.componentregistry.UserUnauthorizedException; … … 9 10 import clarin.cmdi.componentregistry.model.ComponentDescription; 10 11 import static clarin.cmdi.componentregistry.impl.database.ComponentRegistryDatabase.*; 12 import clarin.cmdi.componentregistry.model.AbstractDescription; 11 13 import clarin.cmdi.componentregistry.model.ProfileDescription; 12 14 import clarin.cmdi.componentregistry.model.UserMapping.User; … … 449 451 assertTrue(RegistryTestHelper.hasComponent(xsd, "XXX", "0", "99")); 450 452 } 453 454 @Test 455 public void testUpdate() throws Exception { 456 ComponentRegistry register = getComponentRegistryForUser(null); 457 ComponentDescription description = ComponentDescription. 458 createNewDescription(); 459 description.setName("Aap"); 460 description.setDescription("MyDescription"); 461 462 CMDComponentSpec testComponent = RegistryTestHelper.getTestComponent("Test1"); 463 register.register(description, testComponent); 464 465 // Change values 466 description.setName("Noot"); 467 description.setDescription("AnotherDescription"); 468 // Update in db 469 register.update(description, testComponent); 470 description = register.getComponentDescription(description.getId()); 471 // Test if new values are there 472 assertNotNull(description); 473 assertEquals("Noot", description.getName()); 474 assertEquals("AnotherDescription", description.getDescription()); 475 476 // Update content 477 CMDComponentSpec testComponent2 = RegistryTestHelper.getTestComponent("Test2"); 478 register.update(description, testComponent2); 479 // Test if new content is there 480 assertEquals(RegistryTestHelper.getXml(testComponent2), 481 RegistryTestHelper.getXml(register.getMDComponent(description. 482 getId()))); 483 484 485 // Update both 486 description.setName("Mies"); 487 description.setDescription("YetAnotherDescription"); 488 CMDComponentSpec testComponent3 = RegistryTestHelper.getTestComponent("Test3"); 489 490 // Update in db 491 register.update(description, testComponent3); 492 description = register.getComponentDescription(description.getId()); 493 // Test if new values are there 494 assertNotNull(description); 495 assertEquals("Mies", description.getName()); 496 assertEquals("YetAnotherDescription", description.getDescription()); 497 // Test if new content is there 498 assertEquals(RegistryTestHelper.getXml(testComponent3), 499 RegistryTestHelper.getXml(register.getMDComponent(description. 500 getId()))); 501 } 451 502 } -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java
r1337 r1341 2 2 3 3 import java.io.ByteArrayInputStream; 4 import java.io.IOException; 4 5 import java.io.InputStream; 5 6 import java.io.UnsupportedEncodingException; … … 17 18 import clarin.cmdi.componentregistry.model.ComponentDescription; 18 19 import clarin.cmdi.componentregistry.model.ProfileDescription; 20 import java.io.ByteArrayOutputStream; 19 21 import java.util.regex.Matcher; 20 22 import java.util.regex.Pattern; … … 149 151 } 150 152 153 public static CMDComponentSpec getTestComponent(String name) throws JAXBException { 154 return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(name), MDMarshaller.getCMDComponentSchema()); 155 } 156 157 public static String getXml(CMDComponentSpec componentSpec) throws JAXBException, UnsupportedEncodingException{ 158 ByteArrayOutputStream os = new ByteArrayOutputStream(); 159 MDMarshaller.marshal(componentSpec, os); 160 String xml = os.toString(); 161 try { 162 os.close(); 163 } catch (IOException ex) { 164 } 165 return xml; 166 } 151 167 152 168 -
ComponentRegistry/trunk/ComponentRegistry/src/test/resources/spring-config/database-impl.xml
r1293 r1341 9 9 <import resource="persistence.xml"/> 10 10 11 12 <bean id="componentsCache" class="clarin.cmdi.componentregistry.impl.database.CMDComponentSpecCache" /> 13 <bean id="profilesCache" class="clarin.cmdi.componentregistry.impl.database.CMDComponentSpecCache" /> 11 14 12 15 <!-- Prototype bean for component registry (service) -->
Note: See TracChangeset
for help on using the changeset viewer.