1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.server.core.shared;
22
23
24 import java.time.Duration;
25
26 import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
27 import org.apache.directory.api.ldap.model.name.Dn;
28 import org.apache.directory.api.ldap.model.schema.SchemaManager;
29 import org.apache.directory.server.core.api.DnFactory;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.github.benmanes.caffeine.cache.Cache;
34 import com.github.benmanes.caffeine.cache.Caffeine;
35
36
37
38
39
40
41
42 public class DefaultDnFactory implements DnFactory
43 {
44 private static final Logger LOG = LoggerFactory.getLogger( DefaultDnFactory.class );
45
46
47 private Cache<String, Dn> dnCache;
48
49
50 private SchemaManager schemaManager;
51
52
53 private boolean enableStats = false;
54
55
56 private int hitCount = 0;
57 private int missCount = 0;
58
59
60
61
62
63
64
65
66 public DefaultDnFactory( SchemaManager schemaManager, int cacheSize )
67 {
68 this.schemaManager = schemaManager;
69 this.dnCache = Caffeine.newBuilder().maximumSize( cacheSize ).expireAfterAccess( Duration.ofMinutes( 10L ) )
70 .build();
71 }
72
73
74
75
76
77 @Override
78 public Dn create( String dn ) throws LdapInvalidDnException
79 {
80 if ( dn == null )
81 {
82 return null;
83 }
84
85 if ( dn.trim().length() == 0 )
86 {
87 return Dn.ROOT_DSE;
88 }
89
90 Dn cachedDn = null;
91
92
93
94 if ( dnCache != null )
95 {
96 cachedDn = dnCache.getIfPresent( dn );
97 }
98
99 if ( cachedDn == null )
100 {
101 LOG.debug( "Dn {} not found in the cache, creating", dn );
102
103 cachedDn = new Dn( schemaManager, dn );
104
105 if ( dnCache != null )
106 {
107 dnCache.put( dn, cachedDn );
108 }
109
110 if ( enableStats )
111 {
112 missCount++;
113 }
114 }
115 else
116 {
117 if ( enableStats )
118 {
119 hitCount++;
120 }
121 }
122
123 LOG.debug( "Dn {} found in the cache", dn );
124
125 if ( enableStats )
126 {
127 LOG.debug( "Dn cache hit - {} , miss - {} and is normalized = {}", hitCount, missCount, cachedDn.isSchemaAware() );
128 }
129
130 return cachedDn;
131 }
132
133
134
135
136
137 @Override
138 public Dn create( String... upRdns ) throws LdapInvalidDnException
139 {
140 StringBuilder sb = new StringBuilder();
141 boolean isFirst = true;
142
143 for ( String s : upRdns )
144 {
145 if ( isFirst )
146 {
147 isFirst = false;
148 }
149 else
150 {
151 sb.append( ',' );
152 }
153
154 sb.append( s );
155 }
156
157 String dn = sb.toString();
158
159 return create( dn );
160 }
161
162 }