1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.connector.filesystem;
25
26 import java.io.File;
27 import java.util.Collection;
28 import java.util.Map;
29 import java.util.Set;
30 import org.modeshape.graph.ExecutionContext;
31 import org.modeshape.graph.Location;
32 import org.modeshape.graph.connector.RepositorySourceException;
33 import org.modeshape.graph.property.Name;
34 import org.modeshape.graph.property.Property;
35 import org.modeshape.graph.property.ValueFactory;
36
37 /**
38 * A {@link CustomPropertiesFactory} implementation that throws an exception when attempting to store the "extra" or "custom"
39 * properties for 'nt:file', 'nt:folder', and 'nt:resource' nodes.
40 */
41 public class ThrowProperties extends BasePropertiesFactory {
42
43 private static final long serialVersionUID = 1L;
44
45 /**
46 * Create an instance of this factory.
47 */
48 public ThrowProperties() {
49 }
50
51 /**
52 * {@inheritDoc}
53 *
54 * @see org.modeshape.connector.filesystem.CustomPropertiesFactory#getDirectoryProperties(org.modeshape.graph.ExecutionContext,
55 * org.modeshape.graph.Location, java.io.File)
56 */
57 @Override
58 public Collection<Property> getDirectoryProperties( ExecutionContext context,
59 Location location,
60 File directory ) {
61 return NO_PROPERTIES_COLLECTION;
62 }
63
64 /**
65 * {@inheritDoc}
66 *
67 * @see org.modeshape.connector.filesystem.CustomPropertiesFactory#getFileProperties(org.modeshape.graph.ExecutionContext,
68 * org.modeshape.graph.Location, java.io.File)
69 */
70 @Override
71 public Collection<Property> getFileProperties( ExecutionContext context,
72 Location location,
73 File file ) {
74 return NO_PROPERTIES_COLLECTION;
75 }
76
77 /**
78 * {@inheritDoc}
79 *
80 * @see org.modeshape.connector.filesystem.CustomPropertiesFactory#getResourceProperties(org.modeshape.graph.ExecutionContext,
81 * org.modeshape.graph.Location, java.io.File, java.lang.String)
82 */
83 @Override
84 public Collection<Property> getResourceProperties( ExecutionContext context,
85 Location location,
86 File file,
87 String mimeType ) {
88 return NO_PROPERTIES_COLLECTION;
89 }
90
91 /**
92 * {@inheritDoc}
93 *
94 * @see org.modeshape.connector.filesystem.CustomPropertiesFactory#recordDirectoryProperties(org.modeshape.graph.ExecutionContext,
95 * java.lang.String, org.modeshape.graph.Location, java.io.File, java.util.Map)
96 */
97 @Override
98 public Set<Name> recordDirectoryProperties( ExecutionContext context,
99 String sourceName,
100 Location location,
101 File file,
102 Map<Name, Property> properties ) throws RepositorySourceException {
103 int numProperties = properties.size();
104 if (numProperties != 0) {
105 ValueFactory<String> strings = context.getValueFactories().getStringFactory();
106 StringBuilder names = new StringBuilder();
107 boolean first = true;
108 for (Property property : properties.values()) {
109 if (STANDARD_PROPERTIES_FOR_FILE_OR_FOLDER.contains(property.getName())) continue;
110 if (first) first = false;
111 else names.append(",");
112 String name = strings.create(property.getName());
113 names.append(name);
114 }
115 String msg = null;
116 if (properties.size() == 1) {
117 msg = FileSystemI18n.couldNotStoreProperties.text(names, file.getPath(), sourceName);
118 } else {
119 msg = FileSystemI18n.couldNotStoreProperty.text(names, file.getPath(), sourceName);
120 }
121 throw new RepositorySourceException(sourceName, msg);
122 }
123 return NO_NAMES;
124 }
125
126 /**
127 * {@inheritDoc}
128 *
129 * @see org.modeshape.connector.filesystem.CustomPropertiesFactory#recordFileProperties(org.modeshape.graph.ExecutionContext,
130 * java.lang.String, org.modeshape.graph.Location, java.io.File, java.util.Map)
131 */
132 @Override
133 public Set<Name> recordFileProperties( ExecutionContext context,
134 String sourceName,
135 Location location,
136 File file,
137 Map<Name, Property> properties ) throws RepositorySourceException {
138 int numProperties = properties.size();
139 if (numProperties != 0) {
140 ValueFactory<String> strings = context.getValueFactories().getStringFactory();
141 StringBuilder names = new StringBuilder();
142 boolean first = true;
143 for (Property property : properties.values()) {
144 if (STANDARD_PROPERTIES_FOR_FILE_OR_FOLDER.contains(property.getName())) continue;
145 if (first) first = false;
146 else names.append(",");
147 String name = strings.create(property.getName());
148 names.append(name);
149 }
150 String msg = null;
151 if (properties.size() == 1) {
152 msg = FileSystemI18n.couldNotStoreProperties.text(names, file.getPath(), sourceName);
153 } else {
154 msg = FileSystemI18n.couldNotStoreProperty.text(names, file.getPath(), sourceName);
155 }
156 throw new RepositorySourceException(sourceName, msg);
157 }
158 return NO_NAMES;
159 }
160
161 /**
162 * {@inheritDoc}
163 *
164 * @see org.modeshape.connector.filesystem.CustomPropertiesFactory#recordResourceProperties(org.modeshape.graph.ExecutionContext,
165 * java.lang.String, org.modeshape.graph.Location, java.io.File, java.util.Map)
166 */
167 @Override
168 public Set<Name> recordResourceProperties( ExecutionContext context,
169 String sourceName,
170 Location location,
171 File file,
172 Map<Name, Property> properties ) throws RepositorySourceException {
173 int numProperties = properties.size();
174 if (numProperties != 0) {
175 ValueFactory<String> strings = context.getValueFactories().getStringFactory();
176 StringBuilder names = new StringBuilder();
177 boolean first = true;
178 for (Property property : properties.values()) {
179 if (STANDARD_PROPERTIES_FOR_CONTENT.contains(property.getName())) continue;
180 if (first) first = false;
181 else names.append(",");
182 String name = strings.create(property.getName());
183 names.append(name);
184 }
185 String msg = null;
186 if (properties.size() == 1) {
187 msg = FileSystemI18n.couldNotStoreProperties.text(names, file.getPath(), sourceName);
188 } else {
189 msg = FileSystemI18n.couldNotStoreProperty.text(names, file.getPath(), sourceName);
190 }
191 throw new RepositorySourceException(sourceName, msg);
192 }
193 return NO_NAMES;
194 }
195 }