1   package com.trendmicro.grid.acl.ds.dummy;
2   
3   import com.trendmicro.grid.acl.ds.*;
4   import com.trendmicro.grid.acl.ds.datatypes.SharedPackageFamily;
5   import com.trendmicro.grid.acl.ds.datatypes.SharedVendor;
6   import com.trendmicro.grid.acl.l0.datatypes.NameListPage;
7   import com.trendmicro.grid.acl.l0.datatypes.PackageFamily;
8   import com.trendmicro.grid.acl.metadata.Metadata;
9   import net.sf.tinyjee.concurrent.LockingMap;
10  import org.springframework.stereotype.Repository;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.Map;
15  
16  /**
17   * Implements a dummy in-memory provider for package family.
18   *
19   * @author juergen_kellerer, 2010-05-07
20   * @version 1.0
21   */
22  @Repository
23  public class DummyPackageFamilyRepository implements PackageFamilyRepository, SelectorAware {
24  
25  	Map<String, SharedPackageFamily> familyMap = new LockingMap<String, SharedPackageFamily>();
26  
27  	RepositorySelector<VendorRepository> vendorSelector;
28  
29  	@Override
30  	public void refreshSelectors(RepositorySelectorsRepository selectorsRepository) {
31  		vendorSelector = selectorsRepository.getSelector(VendorRepository.class);
32  	}
33  
34  	@Override
35  	public SharedPackageFamily create(String vendorName, String basename, String displayName, Metadata metadata) {
36  		SharedVendor vendor = vendorSelector.getRepository().getByName(vendorName);
37  		if (vendor == null)
38  			throw new IllegalArgumentException("Vendor '" + vendorName + "' does not exist.");
39  
40  		SharedPackageFamily family = new SharedPackageFamily(vendor, basename, displayName, metadata);
41  		update(family);
42  		return family;
43  	}
44  
45  	@Override
46  	public void update(PackageFamily packageFamily) {
47  		final SharedPackageFamily sharedFamily;
48  		if (packageFamily instanceof SharedPackageFamily) {
49  			sharedFamily = (SharedPackageFamily) packageFamily;
50  		} else {
51  			SharedVendor vendor = vendorSelector.getRepository().getByName(packageFamily.getVendor().getName());
52  			sharedFamily = new SharedPackageFamily(vendor, packageFamily.getBasename(), packageFamily.getDisplayName(), packageFamily.getMetadata());
53  		}
54  
55  		familyMap.put(packageFamily.getBasename(), sharedFamily);
56  	}
57  
58  	@Override
59  	public SharedPackageFamily getByName(String basename) {
60  		return familyMap.get(basename);
61  	}
62  
63  	@Override
64  	public NameListPage listNames(int pageNumber) {
65  		return pageNumber > 0 ? null : new NameListPage(0, true, familyMap.keySet());
66  	}
67  
68  	@Override
69  	public NameListPage listNamesByVendor(String vendorName, int pageNumber) {
70  		if (pageNumber > 0)
71  			return null;
72  
73  		List<String> names = new ArrayList<String>();
74  		for (PackageFamily family : familyMap.values()) {
75  			if (!vendorName.equals(family.getVendor().getName()))
76  				continue;
77  			names.add(family.getBasename());
78  		}
79  
80  		return names.isEmpty() ? null : new NameListPage(0, true, names);
81  	}
82  }